ASP.NET MVC 2 RC 客户端验证不起作用
我似乎无法在 MVC 2 RC 应用程序上进行任何客户端验证。
我的模型具有以下内容:
public class ExampleModel
{
[Required(ErrorMessage="Test1 is required")]
[DisplayName("Test1")]
public string Test1 { get; set; }
[Required(ErrorMessage="Test2 is required")]
[DisplayName("Test2")]
public string Test2 { get; set; }
}
我的视图具有以下代码:
<% Html.EnableClientValidation(); %>
<%= Html.ValidationSummary(true, "Test was unsuccessful.") %>
<% using (Html.BeginForm()) { %>
<div>
<div class="editor-label">Test1:</div>
<div class="editor-field">
<%= Html.TextBoxFor(m => m.Test1) %>
<%= Html.ValidationMessageFor(m => m.Test1) %>
</div>
<div class="editor-label">Test2:</div>
<div class="editor-field">
<%= Html.TextBoxFor(m => m.Test2) %>
<%= Html.ValidationMessageFor(m => m.Test2) %>
</div>
<p>
<input type="submit" value="Test" />
</p>
</div>
我将两个字段留空并单击“测试”按钮,它会直接转到控制器的后处理程序,而不会发生客户端验证。我不确定我错过了什么。
我的视图中还包含以下 javascript(不确定我是否需要全部):
<link href="../../Scripts/jquery-1.3.2.min.js" type="text/javascript" />
<link href="../../Scripts/jquery.validate.min.js" type="text/javascript" />
<link href="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript" />
任何想法我做错了什么。我觉得我错过了一些简单的东西,而且 MVC 2 的文档很少。
编辑:我添加了链接:
我已将该文件包含在我的项目中,我必须从答案中的链接下载该文件。仍然根本不工作。还有其他想法吗?
编辑:我正在使用 Visual Studio 2008 和 MVC 2 RC(不是测试版),并且我正在寻找任何可下载或发布的与 RC 版本一起使用的客户端验证示例。
I can't seem to get any client side validation working on a MVC 2 RC app.
My model has the following:
public class ExampleModel
{
[Required(ErrorMessage="Test1 is required")]
[DisplayName("Test1")]
public string Test1 { get; set; }
[Required(ErrorMessage="Test2 is required")]
[DisplayName("Test2")]
public string Test2 { get; set; }
}
My view has the following code:
<% Html.EnableClientValidation(); %>
<%= Html.ValidationSummary(true, "Test was unsuccessful.") %>
<% using (Html.BeginForm()) { %>
<div>
<div class="editor-label">Test1:</div>
<div class="editor-field">
<%= Html.TextBoxFor(m => m.Test1) %>
<%= Html.ValidationMessageFor(m => m.Test1) %>
</div>
<div class="editor-label">Test2:</div>
<div class="editor-field">
<%= Html.TextBoxFor(m => m.Test2) %>
<%= Html.ValidationMessageFor(m => m.Test2) %>
</div>
<p>
<input type="submit" value="Test" />
</p>
</div>
I leave both fields blank and click the Test button and it goes right to the controller's post handler with no client side validation happening. I am not sure what I am missing.
I have the following javascript also included in the view (not sure if I need it all):
<link href="../../Scripts/jquery-1.3.2.min.js" type="text/javascript" />
<link href="../../Scripts/jquery.validate.min.js" type="text/javascript" />
<link href="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript" />
Any ideas what I am doing wrong. I feel like I am missing something simple and the documentation for MVC 2 is sparse.
Edit: I have added the link:
<link href="../../Scripts/MicrosoftMvcJQueryValidation.js" type="text/javascript" />
And I have included the file in my project which I had to download from on of the links in the answers. Still not working at all. Any other ideas?
Edit: I am using Visual Studio 2008 with MVC 2 RC (not beta) and I am looking for any downloadable or posted examples of client-side validation working with the RC release.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您从 MVC 2 Beta 更新项目时,请使用:MVC 2 RC 源代码包 (链接)。旧的 Beta 版本无法与 RC 中的 jquery.validation 正常工作。所需的 javascript 文件是:
MicrosoftMvcJQueryValidation.js 的正确版本包含此 $(document).ready() 函数:
位于文件末尾(在 RC 版本中)。
When you update project from MVC 2 Beta, use:
/src/MvcFutures/MicrosoftMvcJQueryValidation.js
from MVC 2 RC Source Code Package (link). Older Beta version do not work correctly with jquery.validation in RC. Needed javascript files are:Right version of MicrosoftMvcJQueryValidation.js contains this $(document).ready() function:
at the end of file (in RC release).
好吧,我明白了……这 100% 是我的错。尽管如此,一些帖子也包含了我确实需要的一些信息。
主要问题是我的 HTML 包含了脚本,我很惊讶没有人注意到这个问题...查看我的帖子,看看是否可以看到问题。
我使用的是
标签而不是正确的
无论如何,所需的正确链接是:
然后一切正常。您肯定需要包含 futures 项目中的“MicrosoftMvcJQueryValidation.js”文件,因此我对所有提到这一点的帖子都投了赞成票。
开箱即用,但不包含该文件。如果您不担心使用 JQuery,那么您可以使用以下包含内容来使用 Microsoft 实现,该实现将与 RC 一起开箱即用:
我希望我可以至少为一个人节省一些悲伤...我仍然不能相信我怎么可能搞砸了包含物并且很长时间没有注意到它。
再次感谢您的所有帮助。
Ok I figured this out... and it is 100% my fault. Although, a couple of the posts included some information that I did need also.
The main problem, which I am suprised no one noticed, was my HTML to include the scripts... look up at my post and see if you can see the problem.
I was using a
<link href=...
tag instead of the proper<script src=...
tag. Totally my fault as I had quickly cut and pasted the CSS link without thinking and just changed the type and file. Duh!!!Anyways the correct links required are:
Everything works then. You definately do need to include the 'MicrosoftMvcJQueryValidation.js' file from the futures project so I am upvoted all the posts that mentioned that.
Out of the box though that file is NOT included. If your not worried about using JQuery then you can just use the following includes to use the Microsoft implementation which will work out of the box with the RC:
I hope I can save at least one person some grief... I still can't believe how I could have screwed up the include and not noticed it for sooooo long.
Thanks again for all your help.
您必须包含 MicrosoftMvcJQueryValidation.js 文件:
检查一下:MicrosoftMvcJQueryValidation.js 的正确版本在哪里MVC 2 beta 2?
接下来只需将
Html.EnableClientValidation();
放置在视图页面中的某个位置即可。它必须位于您想要客户端验证的第一个表单之前。我更喜欢 Site.Master 页面。
致力于 ASP.NET MVC 2 RC。
You have to include MicrosoftMvcJQueryValidation.js file :
<link href="MicrosoftMvcJQueryValidation.js" type="text/javascript" />
Check this: Where is the right version of MicrosoftMvcJQueryValidation.js for MVC 2 beta 2?
Next just put
Html.EnableClientValidation();
somewhere in the View page.It must be before the first form you want to client-side validate. I prefer Site.Master page.
Working on ASP.NET MVC 2 RC.
您确定包含了正确的 JS 文件吗?因为在 Phill Haack 的帖子 中,它有
附加 MicrosoftMvcJQueryValidation.js
,而不是MicrosoftMvcValidation.js
。他还在视图中设置了 ClientValidationFunction 属性:
尽管这不是 RC,而是 Beta。
Are you sure you included the correct JS files? Because in Phill Haack's post it has
MicrosoftMvcJQueryValidation.js
attached instead ofMicrosoftMvcValidation.js
.He also sets the
ClientValidationFunction
property in the view :Though that was not RC, but Beta.
ASP.NET MVC 2 Release Candidate 中的默认(且仅受 Microsoft 支持)验证系统不使用 jQuery Validate。相反,它使用完全存在于 MicrosoftMvcValidation.js 中的新验证系统(尽管您还需要包含 MicrosoftAjax.js)。
如果您想使用 jQuery Validate 库,它作为 ASP.NET MVC Futures 项目的一部分包含在内(可用 这里),这是一个单独的下载,并且有自己的适配器脚本文件。
另外,关于
Html.ValidationSummary()
帮助器,我相信如果您希望它具有新的客户端功能,则需要将其包含在表单中。如果它在表单之外,它仍然可以工作,但它只能在服务器端工作。The default (and only supported by Microsoft) validation system in the ASP.NET MVC 2 Release Candidate does not use jQuery Validate. Instead, it uses a new validation system that exists entirely in MicrosoftMvcValidation.js (though you do also need to include MicrosoftAjax.js).
If you want to use the jQuery Validate library, it is included as part of the ASP.NET MVC Futures project (available here), which is a separate download, and has its own adapter script file.
Also, regarding the
Html.ValidationSummary()
helper, I believe it needs to be included inside the form if you want it to have the new client-side functionality. It will still work if it's outside of the form, but it will only work server-side.