asp.net-mvc ASP.NET MVC 3:对动态/AJAX 内容进行不显眼的客户端验证所需的步骤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8538082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC 3: Required steps for unobtrusive client-side validation of dynamic/AJAX content
提问by Kaleb Pederson
What are the complete set of Steps Requiredfor client-side unobtrusive validation to work for dynamically inserted form fields?
客户端非侵入式验证适用于动态插入的表单字段所需的完整步骤集是什么?
Relevant SO posts
相关SO帖子
ASP.NET MVC 3 unobtrusive client-side validation with dynamic content- He needed the unobtrusive validation attributes to show up in the generated HTML and did so by calling BeginForm
ASP.NET MVC 3 unobtrusive client-side validation with dynamic content- 他需要不显眼的验证属性显示在生成的 HTML 中,并通过调用BeginForm
ASP.Net MVC 3 validation on AjaxForm- The asker was using Ajax.BeginFormwhich uses MicrosoftAjax instead of JQuery.validation.
AjaxForm 上的 ASP.Net MVC 3 验证- 提问者使用的Ajax.BeginForm是 MicrosoftAjax 而不是 JQuery.validation。
PartialView and unobtrusive client validation not working- He had the problem with unobtrusive validation attributes not showing up and overrode ViewContext.FormContextas a workaround.
PartialView 和不显眼的客户端验证不起作用- 他遇到了不显眼的验证属性没有显示和覆盖ViewContext.FormContext作为解决方法的问题。
ASP.NET MVC 3: Generate unobtrusive validation when BeginForm is on the layout- Workaround for unobtrusive validation attributes not showing up in HTML
ASP.NET MVC 3:当 BeginForm 在布局上时生成不显眼的验证- 不显眼的验证属性未显示在 HTML 中的解决方法
Relevant Links
相关链接
Brad Wilson's Unobtrusive Client Validation in ASP.NET MVC 3
Brad Wilson在 ASP.NET MVC 3 中的不显眼的客户端验证
The Complete Guide To Validation In ASP.NET MVC 3 - Part 1
ASP.NET MVC 3 中的完整验证指南 - 第 1 部分
The Complete Guide To Validation In ASP.NET MVC 3 - Part 2
ASP.NET MVC 3 中的完整验证指南 - 第 2 部分
Unobtrusive Client-side Validation with Dynamic Contents in ASP.NET MVC 3
回答by Kaleb Pederson
At this point I believe the following is a complete set of requirements:
至此,我认为以下是一套完整的要求:
- Create a form with
Html.BeginForm - Turn on
ClientValidationEnabled - Turn on
UnobtrusiveJavaScriptEnabled - Set appropriate validation attributes on the model's properties (notfields)
- If the Html Helpers being used to create the form elements are not on the same form as the
Html.BeginFormcall, use a relevant workaround (see workaround 1and workaround 2) - Include
jquery,jquery.validate.js, andjquery.validate.unobtrusive.jsfiles, in that order - Verify that the unobtrusive validation attributes are present in the HTML
- If using custom validators:
- ensure that they are added to
jQuery.validator.unobtrusive.adapters - ensure that they are added to the jQuery validation plugin by calling
jQuery.validator.addMethod. - ensure that the above happen before
$(document).ready()as at that point it's too late
- ensure that they are added to
- Call
jQuery.validator.unobtrusive.parseorjQuery.validator.unobtrusive.parseElementon elements added dynamically after the initial page load.
- 创建一个表单
Html.BeginForm - 打开
ClientValidationEnabled - 打开
UnobtrusiveJavaScriptEnabled - 在模型的属性(不是字段)上设置适当的验证属性
- 如果用于创建表单元素的 Html Helpers 与
Html.BeginForm调用不在同一个表单上,请使用相关的解决方法(请参阅解决方法 1和解决方法 2) - 按此顺序包含
jquery、jquery.validate.js和jquery.validate.unobtrusive.js文件 - 验证 HTML 中是否存在不显眼的验证属性
- 如果使用自定义验证器:
- 确保它们被添加到
jQuery.validator.unobtrusive.adapters - 确保通过调用将它们添加到 jQuery 验证插件中
jQuery.validator.addMethod。 - 确保上述情况发生之前
$(document).ready(),因为那时为时已晚
- 确保它们被添加到
- 调用
jQuery.validator.unobtrusive.parse或jQuery.validator.unobtrusive.parseElement在初始页面加载后动态添加的元素。

