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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:37:58  来源:igfitidea点击:

ASP.NET MVC 3: Required steps for unobtrusive client-side validation of dynamic/AJAX content

asp.net-mvcunobtrusive-validation

提问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

在 ASP.NET MVC 3 中使用动态内容进行不显眼的客户端验证

回答by Kaleb Pederson

At this point I believe the following is a complete set of requirements:

至此,我认为以下是一套完整的要求:

  1. Create a form with Html.BeginForm
  2. Turn on ClientValidationEnabled
  3. Turn on UnobtrusiveJavaScriptEnabled
  4. Set appropriate validation attributes on the model's properties (notfields)
  5. 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)
  6. Include jquery, jquery.validate.js, and jquery.validate.unobtrusive.jsfiles, in that order
  7. Verify that the unobtrusive validation attributes are present in the HTML
  8. 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
  9. Call jQuery.validator.unobtrusive.parseor jQuery.validator.unobtrusive.parseElementon elements added dynamically after the initial page load.
  1. 创建一个表单 Html.BeginForm
  2. 打开 ClientValidationEnabled
  3. 打开 UnobtrusiveJavaScriptEnabled
  4. 在模型的属性(不是字段)上设置适当的验证属性
  5. 如果用于创建表单元素的 Html Helpers 与Html.BeginForm调用不在同一个表单上,请使用相关的解决方法(请参阅解决方法 1解决方法 2
  6. 按此顺序包含jqueryjquery.validate.jsjquery.validate.unobtrusive.js文件
  7. 验证 HTML 中是否存在不显眼的验证属性
  8. 如果使用自定义验证器:
    • 确保它们被添加到 jQuery.validator.unobtrusive.adapters
    • 确保通过调用将它们添加到 jQuery 验证插件中jQuery.validator.addMethod
    • 确保上述情况发生之前$(document).ready(),因为那时为时已晚
  9. 调用jQuery.validator.unobtrusive.parsejQuery.validator.unobtrusive.parseElement在初始页面加载后动态添加的元素。