不与动态内容的工作不显眼的审定 [英] unobtrusive validation not working with dynamic content
问题描述
我在试图让不显眼的jQuery验证与通过AJAX调用动态加载的局部视图的工作问题。
I'm having problems trying to get the unobtrusive jquery validation to work with a partial view that is loaded dynamically through an AJAX call.
我已经花了几天试图让这个code,没有运气的工作。
I've been spending days trying to get this code to work with no luck.
这里的景观:
@model MvcApplication2.Models.test
@using (Html.BeginForm())
{
@Html.ValidationSummary(true);
<div id="res"></div>
<input id="submit" type="submit" value="submit" />
}
局部视图:
@model MvcApplication2.Models.test
@Html.TextAreaFor(m => m.MyProperty);
@Html.ValidationMessageFor(m => m.MyProperty);
<script type="text/javascript" >
$.validator.unobtrusive.parse(document);
</script>
示范:
public class test
{
[Required(ErrorMessage= "required field")]
public int MyProperty { get; set; }
}
控制器:
public ActionResult GetView()
{
return PartialView("Test");
}
和最后的JavaScript:
and finally, the javascript:
$(doument).ready(function () {
$.ajax({
url: '/test/getview',
success: function (res) {
$("#res").html(res);
$.validator.unobtrusive.parse($("#res"));
}
});
$("#submit").click(function () {
if ($("form").valid()) {
alert('valid');
return true;
} else {
alert('not valid');
return false;
}
});
验证不起作用。即使我不填写texbox的任何信息,提交事件显示警报(有效)。
The validation does not work. Even if I don't fill any information in the texbox, the submit event shows the alert ('valid').
然而,如果不是加载动态视图,我用 @ Html.Partial(测试,模型)
渲染主视图的局部视图(和我不这样做AJAX调用),那么验证工作就好了。
However, if instead of loading dynamically the view, I use @Html.Partial("test", Model)
to render the partial View in the main View (and I don't do the AJAX call), then the validation works just fine.
这可能是因为,如果我动态加载的内容,该控件不会在DOM存在呢。但是我做的 $呼叫validator.unobtrusive.parse($(#RES));
这应该足以让有关新加载控件验证。 ..
This is probably because if I load the content dynamically, the controls don't exist in the DOM yet. But I do a call to $.validator.unobtrusive.parse($("#res"));
which should be enough to let the validator about the newly loaded controls...
谁能帮助?
推荐答案
如果你试图解析已解析它不会更新表单
If you try to parse a form that is already parsed it won't update
当您添加动态元素的形式,你可以做的是不是
What you could do when you add dynamic element to the form is either
1 - 你可以清除表格的验证和重新验证它
它会是这样
1- You could remove the form's validation and re validate it it would be like this
var form = $(formSelector)
.removeData("validator") /* added by the raw jquery.validate plugin */
.removeData("unobtrusiveValidation"); /* added by the jquery unobtrusive plugin */
$.validator.unobtrusive.parse(form);
2 - 访问使用jQuery表单的 unobtrusiveValidation
数据数据
办法( $(形式)。数据(unobtrusiveValidation')
)
并访问规则集,并添加新的元素属性(这有点复杂)
2- Access the form's unobtrusiveValidation
data using the jquery data
method ($(form).data('unobtrusiveValidation')
)
and access the rules collection and add the new elements attributes (which is somewhat complicated)
检查这篇文章,这是用于添加动态元素的表单插件
<一href=\"http://xhalent.word$p$pss.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/\">http://xhalent.word$p$pss.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/
check this article, this is a plugin used for adding dynamic elements to a form http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/
PS:这个插件使用,我指出了第二的解决方案
ps: This plugin uses the 2nd solution that i pointed out
这篇关于不与动态内容的工作不显眼的审定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!