ASP.Net MVC 3 不引人注目的验证不适用于部分视图
我已经设置了一个部分视图,其中包含自己的表单标签,如下所示:
<tr>
@using (Html.BeginForm("Create"))
{
<td>
@Html.TextBoxFor(model => model.Date)
@Html.ValidationMessageFor(model => model.Date)
</td>
<td>
@Html.TextBoxFor(model => model.Amount)
@Html.ValidationMessageFor(model => model.Amount)
</td>
<td>
@Html.TextBoxFor(model => model.Tags)
@Html.ValidationMessageFor(model => model.Tags)
</td>
<td>
@Html.EnumDropDownListFor(model => model.Type)
</td>
<td>
<input type="submit" value="Add" />
@Html.ValidationSummary(true)
</td>
}
</tr>
我使用 @Html.Action("Create") 将其渲染在页面上(它是表格的一部分,因此是 标签。
对于某些人来说奇怪的原因是我的客户端验证不起作用,我在发布时首先看到错误。
部分视图和客户端验证有什么特别之处吗?
我已经包含了以下脚本:
<script src="/Scripts/jquery.1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
编辑
我只是尝试扔掉这个。脚本到页面上:
jQuery('form').submit(function ()
{
alert(jQuery(this).valid());
});
它警告“true”,因此客户端验证脚本肯定在那里,并且由于某种原因它没有检查有问题的字段:-/
编辑2
我已经上传了整个源代码要粘贴的页面代码(HTML + JS): http://pastebin.com/GvqLW495
I've setup a partial view which houses its own form tag, like so:
<tr>
@using (Html.BeginForm("Create"))
{
<td>
@Html.TextBoxFor(model => model.Date)
@Html.ValidationMessageFor(model => model.Date)
</td>
<td>
@Html.TextBoxFor(model => model.Amount)
@Html.ValidationMessageFor(model => model.Amount)
</td>
<td>
@Html.TextBoxFor(model => model.Tags)
@Html.ValidationMessageFor(model => model.Tags)
</td>
<td>
@Html.EnumDropDownListFor(model => model.Type)
</td>
<td>
<input type="submit" value="Add" />
@Html.ValidationSummary(true)
</td>
}
</tr>
I render it on a page using @Html.Action("Create") (It's part of a table, hence the <tr> tags.
For some odd reason my clientside validation isn't working, and I first see the errors upon posting.
Is there something special about partial views and clientside validation ?
I have included the following scripts:
<script src="/Scripts/jquery.1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
EDIT
I just tried tossing this script onto the page:
jQuery('form').submit(function ()
{
alert(jQuery(this).valid());
});
It alerts 'true', so the clientside validation script is definately there, and for some reason it's not checking the fields in question :-/
EDIT 2
I've uploaded the entire source code for the page (the HTML + JS) to pastebin: http://pastebin.com/GvqLW495
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:
我刚刚意识到,查看您的代码,您正在使用 jQuery 1.5.1 和(我假设).NET 提供的 jQuery.validate。不幸的是,这两个人还没有一起工作。您必须前往此处获取适用于最新 jQuery 的版本(您需要使用 1.4.4)。如果这不起作用,我仍然建议查看下面的解决方案。
我有一个类似的问题(尽管我不确定这是完全相同的问题)。我写了关于解决方案 在这篇博文中。不幸的是,我不能确定你是否也遇到同样的问题,但值得一试。
基本上,它归结为这样一个事实:我必须在加载 PartialViews 后调用这一行(尽管我是通过 AJAX 加载它们,我认为这就是导致问题的原因):
有关更多详细信息,请参阅博客文章。希望它能帮助你。
Edit:
I just realized, looking at your code, that you're using jQuery 1.5.1 with the (I'm assuming) .NET provided jQuery.validate. Unfortunately, those two do not work together yet. You'll have to head to here to grab a version that works with the latest jQuery (you'll need to use 1.4.4). If that doesn't work, I would still recommend checking out the solution below.
I had a similar problem (although I'm not sure it's the exact same problem). I wrote about the solution in this blog post. Unfortunately, I can't be sure you're having the same exact problem, but it's worth a shot.
Basically, it boiled down to the fact that I had to call this line after loading my PartialViews (although I was loading them via AJAX which is what I think caused the problem):
See the blog post for more detail. Hopefully it helps you out.
我终于找到了导致它失败的原因,事实上我的部分视图位于 html 表内...
这不起作用,但是如果我将 @Html.Action 移到表标记之外,它就可以正常工作。
I finally found out what's causing it to fail, it's the fact that my partial view is inside a html table...
This doesn't work, however if I move the @Html.Action outside the table tag, it works just fine.
我认为你的问题的根源是使用非法的 html 语法:
标签只能包含
标签。
在大多数情况下,像您这样的结构不会在浏览器中正确呈现,尤其是异步加载时。
I think that the root of your problem wat the use of illegal html syntax:
<tr>
tag can only contain<td>
tags.In most cases structure like your's won't be rendered correcty in browsers, especiallywhen loaded asynchronously.