MVC 4客户端验证不适用于使用Ajax加载的表单。

时间:2022-12-09 10:15:07

I have an Admin page in which the user clicks on links and the corresponding PartialView, containing a web form is then loaded inside a particular div on the Admin page using Ajax.

我有一个管理页面,用户在其中单击链接和相应的PartialView,其中包含一个web表单,然后使用Ajax将其加载到管理页面上的特定div中。

All of the

所有的

"~/Scripts/jquery-2.0.3.js",
"~/Scripts/jquery.unobtrusive-ajax.js",
"~/Scripts/jquery.validate.js",
"~/Scripts/jquery.validate.unobtrusive.js"

are referenced within the Admin page and when the PartialView is loaded, the jQuery client side validation won't work.

在管理页面中引用,当加载PartialView时,jQuery客户端验证将不起作用。

but when I reference those scripts within the PartialView, everything works just fine but I don't intend to do this for each PartialView because they are numerous and each time each one loads, at least two of those .js files must be requested from the server again.

但是当我在PartialView中引用这些脚本时,一切都很好,但是我不打算对每个PartialView都这么做,因为每次加载一个脚本时,至少有两个.js文件必须再次从服务器请求。

Is there any way I can have those scripts inside my parent (Admin) page without this issue ?

有没有什么办法可以让这些脚本在我的父(管理)页没有这个问题?

2 个解决方案

#1


47  

You need this on each one of your partial views:

你的每一个观点都需要这样:

$(document).ready(function () {

    $.validator.unobtrusive.parse("#YourFormID");

});

Basically the validation is not bound on the dynamically rendered form...

基本上,验证不会绑定到动态呈现的表单上……

#2


1  

Basically the validator parses the elements on document ready. You can call it on your own if you like, however someone has already posted a question about this. and the accepted answer probably still works. One of the answers has a blog post link for further reading if you like.

基本上,验证器解析文档中的元素。如果你愿意,你可以自己打电话,但是有人已经就此问题发了一个帖子。公认的答案可能仍然有效。其中一个答案有一个博客链接供你进一步阅读。

client side validation with dynamically added field

使用动态添加字段的客户端验证

#1


47  

You need this on each one of your partial views:

你的每一个观点都需要这样:

$(document).ready(function () {

    $.validator.unobtrusive.parse("#YourFormID");

});

Basically the validation is not bound on the dynamically rendered form...

基本上,验证不会绑定到动态呈现的表单上……

#2


1  

Basically the validator parses the elements on document ready. You can call it on your own if you like, however someone has already posted a question about this. and the accepted answer probably still works. One of the answers has a blog post link for further reading if you like.

基本上,验证器解析文档中的元素。如果你愿意,你可以自己打电话,但是有人已经就此问题发了一个帖子。公认的答案可能仍然有效。其中一个答案有一个博客链接供你进一步阅读。

client side validation with dynamically added field

使用动态添加字段的客户端验证