动态内容ASP.NET MVC 3不显眼的客户端验证 [英] ASP.NET MVC 3 unobtrusive client-side validation with dynamic content
问题描述
我已经启用了不显眼的客户端验证我的应用程序,我有这个模型(类似的东西):
I have enabled unobtrusive client side validation in my app, and I have (something similar) to this model:
public class MyModel
{
[Required]
public string Name { get; set; }
[Range(0, 100)]
public int? Age { get; set; }
}
我有以下/首页/索引视图:
I have the following /Home/Index view:
@model MyModel
@using (Html.BeginForm("Test", "Home"))
{
<div id="my-form">
@Html.Partial("Model")
</div>
<input type="submit" value="Go" />
}
随着部分模式:
@model MyModel
<div>
@Html.LabelFor(x => x.Name)
@Html.TextBoxFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name)
</div>
<div>
@Html.LabelFor(x => x.Age)
@Html.TextBoxFor(x => x.Age)
@Html.ValidationMessageFor(x => x.Age)
</div>
一切都是白鬼,鲂,我得到很好的验证错误消息。
Everything is honky-dory, I get nice validation error messages.
现在我要加载的形式/部分模型动态的,即遥控器上的@ Html.Partial并添加以下内容:
Now I want to load the form / partial "Model" dynamically, i.e. remote the @Html.Partial and add the following:
<a href="#" id="load-form">Load the form</a>
<script type="text/javascript">
$(function () {
$("#load-form").click(function () {
$.post(
"/Home/Test",
null,
function (result) {
$("#my-form").html(result);
$.validator.unobtrusive.parse($("#my-form"));
},
"html");
});
});
</script>
在哪里/主页/测试是简单返回部分(模型,新为MyModel());
但现在的客户端验证不再工作了,如果我看着DOM和HTML返回的数据 - 属性的不可以渲染,即:
But now the client side validation is no longer working, and if I look at the DOM and HTML returned, the data- attributes are not rendered, i.e.:
随着Html.Partial()在/ home /指数
<div>
<label for="Name">Name</label>
<input data-val="true" data-val-required="The Name field is required." id="Name" name="Name" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>
</div>
<div>
<label for="Age">Age</label>
<input data-val="true" data-val-number="The field Age must be a number." data-val-range="The field Age must be between 0 and 100." data-val-range-max="100" data-val-range-min="0" id="Age" name="Age" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Age" data-valmsg-replace="true"></span>
</div>
XHR响应
<div>
<label for="Name">Name</label>
<input id="Name" name="Name" type="text" value="" />
</div>
<div>
<label for="Age">Age</label>
<input id="Age" name="Age" type="text" value="" />
</div>
即使我添加 Html.EnableClientValidation();
和 Html.EnableUnobtrusiveJavaScript();
明确在部分查看验证的东西是不渲染。
Even if I add Html.EnableClientValidation();
and Html.EnableUnobtrusiveJavaScript();
explicitly in the partial view the validation stuff is not rendered.
如何让我的AJAX部分呈现验证的属性和信息?或者还有什么我可能是做错了吗?
How do I make my AJAX partial render the validation attributes and messages? Or what else could I be doing wrong?
推荐答案
啊......简单:)
Ahh... simple :)
的 @ Html.BeginForm()
必须在相同的部分(我猜是很公平的,因为我们正在验证表单),因此:
The @Html.BeginForm()
must be in the same partial (which I guess is fair enough, since we are validating on a form), so:
/首页/索引视图就变成了:
/Home/Index view becomes:
@model MyModel
<div id="my-form">
@Html.Partial("Model")
</div>
随着部分模式:
@model MyModel
@using (Html.BeginForm("Test", "Home"))
{
<div>
@Html.LabelFor(x => x.Name)
@Html.TextBoxFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name)
</div>
<div>
@Html.LabelFor(x => x.Age)
@Html.TextBoxFor(x => x.Age)
@Html.ValidationMessageFor(x => x.Age)
</div>
<input type="submit" value="Go" />
}
这篇关于动态内容ASP.NET MVC 3不显眼的客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!