I'm using the client validation function of the MVC 2.0 framework (with Html.ValidationMessageFor()
and Html.EnableClientValidation()
).
我正在使用MVC 2.0框架的客户端验证函数(使用Html.ValidationMessageFor()和Html.EnableClientValidation()))。
Everything is nice, when I use the validation in a simple form.
当我以简单的形式使用验证时,一切都很好。
But when I get this form via jQuery Ajax
但是当我通过jQuery Ajax获得这个表单时。
$.get('PathToMyForm', function(htmlResult) {
$('selector').html(htmlResult);
});
client validation doesn't work. Why?
客户端验证行不通。为什么?
3 个解决方案
#1
0
I've had problems with MVC validation and partial views too. I sorted it out by using jquery.validate.js instead of the build-in client-validation. You can try that out.
我也遇到过MVC验证和部分视图的问题。我用jquery.validate来解决它。而不是内建的客户端验证。你可以试试。
#2
7
If you are using jquery.validate (particularly with MVC) and you are loading pages via AJAX, you need to make the following call after the page loads:
如果您正在使用jquery。验证(特别是使用MVC),当您通过AJAX加载页面时,需要在页面加载后进行如下调用:
$.validator.unobtrusive.parse($("#validation"));
See more at my blog post: Using Unobtrusive jQuery Validation with Forms Loaded via AJAX
在我的博客文章中可以看到更多:使用不显眼的jQuery验证,通过AJAX加载表单。
#3
0
Maybe jQuery isn't evaluating the JavaScript code on the Ajax response?
也许jQuery没有在Ajax响应上评估JavaScript代码?
Try using dataType property on the Ajax call,
尝试在Ajax调用上使用dataType属性,
$.get('PathToMyForm', {dataType 'html'}, function(htmlResult) {
$('selector').html(htmlResult);
});
From the jQuery documentation:
从jQuery文档:
dataType Default: Intelligent Guess (xml, json, script, or html)
默认数据类型:智能猜测(xml、json、脚本或html)
The type of data that you're expecting back from the server. If none is specified, jQuery will intelligently try to get the results, based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).
您期望从服务器返回的数据类型。如果没有指定,jQuery将明智地试图获得结果,基于响应的MIME类型(XML MIME类型将产生XML、JSON将产生一个JavaScript对象,在1.4在1.4脚本将执行脚本,和其他任何会返回一个字符串)。
#1
0
I've had problems with MVC validation and partial views too. I sorted it out by using jquery.validate.js instead of the build-in client-validation. You can try that out.
我也遇到过MVC验证和部分视图的问题。我用jquery.validate来解决它。而不是内建的客户端验证。你可以试试。
#2
7
If you are using jquery.validate (particularly with MVC) and you are loading pages via AJAX, you need to make the following call after the page loads:
如果您正在使用jquery。验证(特别是使用MVC),当您通过AJAX加载页面时,需要在页面加载后进行如下调用:
$.validator.unobtrusive.parse($("#validation"));
See more at my blog post: Using Unobtrusive jQuery Validation with Forms Loaded via AJAX
在我的博客文章中可以看到更多:使用不显眼的jQuery验证,通过AJAX加载表单。
#3
0
Maybe jQuery isn't evaluating the JavaScript code on the Ajax response?
也许jQuery没有在Ajax响应上评估JavaScript代码?
Try using dataType property on the Ajax call,
尝试在Ajax调用上使用dataType属性,
$.get('PathToMyForm', {dataType 'html'}, function(htmlResult) {
$('selector').html(htmlResult);
});
From the jQuery documentation:
从jQuery文档:
dataType Default: Intelligent Guess (xml, json, script, or html)
默认数据类型:智能猜测(xml、json、脚本或html)
The type of data that you're expecting back from the server. If none is specified, jQuery will intelligently try to get the results, based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).
您期望从服务器返回的数据类型。如果没有指定,jQuery将明智地试图获得结果,基于响应的MIME类型(XML MIME类型将产生XML、JSON将产生一个JavaScript对象,在1.4在1.4脚本将执行脚本,和其他任何会返回一个字符串)。