我看到一个类似的帖子,试图用晚餐的例子做同样的事情,但他解决了他的问题,我的问题似乎更深一些。基本上,我可以让验证工作得很好,但它只能在Firefox中工作。在IE7中,当页面加载时,我立即得到一个警告框,其中包含以下消息:"Error: Element title is not In a form"...显然,它在这里的形式,如果需要,我可以张贴标记,实际上是从视图源代码呈现,以显示这一点。任何关于我能做什么来解决这个问题的想法都将不胜感激!
基本上,我只是想确保我的NewsPost有标题和正文。因为我把它包装在ViewModel中,所以我想IE不太理解这一点。也许我错了。
我正在使用xVal进行验证。我传入一个ViewModel作为我的模型。我的ViewModel看起来像这样:
public class NewsAdminViewData : ViewModel
{
public NewsPost NewsPost { get; set; }
public List<SelectListItem> NewsItem { get; set; }
public List<SelectListItem> NewsGroup { get; set; }
public NewsAdminViewData(List<SelectListItem> newsItem, List<SelectListItem> newsGroup, NewsPost newsPost)
{
this.NewsItem = newsItem;
this.NewsGroup = newsGroup;
this.NewsPost = newsPost;
}
}这是我的观点:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVCApp.Models.ViewModels.News.NewsAdminViewData>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm())
{%>
<div class="moduleContainer">
<div class="moduleTitle">
Create News Item
</div>
<div class="moduleContent">
<div>
<div>
Title:</div>
<div>
<%= Html.TextBox("Title") %>
</div>
</div>
<div>
<div>
</div>
<div>
<%= Html.TextArea("Body") %>
</div>
</div>
<div>
<div>
News Group:
</div>
<div>
<%= Html.DropDownList("NewsGroup")%>
</div>
</div>
<div>
<div>
News Item:
</div>
<div>
<%= Html.DropDownList("NewsItem") %>
</div>
</div>
</div>
<div class="moduleFooter">
<%= Html.SubmitButton("btnSubmit", "Add News Post", null, "To add this news post click here.", "#ffd40f")%>
</div>
</div>
<% } %>
<%= Html.ClientSideValidation<NewsPost>()%>最后是我的post action:
[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Create(/*FormCollection collection*/ NewsPost np)
{
NewsPost entity = this.reposNewsPost.New();
try
{
entity.Title = np.Title;
entity.NewsPostGUID = System.Guid.NewGuid();
entity.DateAdded = DateTime.Now;
entity.DateUpdated = DateTime.Now;
entity.Body = np.Body;
UpdateModel(entity);
this.reposNewsPost.Insert(entity);
this.reposNewsPost.SubmitChanges();
return RedirectToAction("Index");
}
catch (RulesException ex)
{
ex.AddModelStateErrors(ModelState, "NewsPost");
return ModelState.IsValid ? RedirectToAction(MVC.News.Actions.Create)
: (ActionResult)View();
}
}发布于 2009-10-21 21:08:47
我遇到了和你类似的问题。老实说,看起来你不像我一样弄得一团糟,但也许它会指引你找到解决方案。
带有xVal客户端验证的Mine输入表单被动态加载(jquery ajax调用)到页面中。不幸的是,我忽略了我将返回的html放入了在父元素之间已经有'form‘的元素中。因为它是一个ajax请求,所以嵌套的“表单”元素出现在页面上,而xVal方法"_attachRuleToDOMElement“是这样实现的:
var parentForm = element.parents("form");
if (parentForm.length != 1)// <-- there can be only one parent form of course
alert("Error: Element " + element.attr("id") + " is not in a form");我的parentForm.lengts是2!
我看到你这里肯定没有嵌套的表单,但也许在母版页中有一些东西?可能是IE7呈现嵌套的表单,而Firefox不呈现。另外,我会用不同的名称来命名输入控件,尽管这不应该是问题的根源:
<%= Html.TextBox("np.Title") %>...<%= Html.TextBox("np.Body") %> etc...然后我也会设置elementPrefix:
<%= Html.ClientSideValidation<NewsPost>("np")%>https://stackoverflow.com/questions/1531868
复制相似问题