从 AJAX 调用追加表单时如何正确设置 MVC 5 非侵入式验证?
How to setup MVC 5 unobtrusive validation correctly when appending the form from an AJAX call?
我有关于这个问题的 googeld,我已经检查了我的 web.config、bundleconfig 和我的布局,如下所示:
web.config:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
在我 "BundleConfig.cs" 下的 App_Start 文件夹中:
var jqueryBundle = new ScriptBundle("~/bundles/jquery");
jqueryBundle.Include("~/Scripts/jquery-{version}.js");
jqueryBundle.Include("~/Scripts/moment.min.js");
jqueryBundle.Include("~/Scripts/loadingoverlay.js");
jqueryBundle.Include("~/Scripts/fullcalendar.js");
jqueryBundle.Include("~/Scripts/lang-all.js");
jqueryBundle.Transforms.Add(jsTransformer);
jqueryBundle.Orderer = nullOrderer;
bundles.Add(jqueryBundle);
var jqueryvalBundle = new ScriptBundle("~/bundles/jqueryval");
jqueryvalBundle.Include("~/Scripts/jquery.validate*");
jqueryvalBundle.Include("~/Scripts/jquery.validate.js");
jqueryvalBundle.Include("~/Scripts/jquery.validate.unobtrusive.js");
jqueryvalBundle.Transforms.Add(jsTransformer);
jqueryvalBundle.Orderer = nullOrderer;
bundles.Add(jqueryvalBundle);
在我的布局页面中:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
Firebug 显示:
到目前为止,一切都包括在内,应该会 运行 顺利进行。
我的模型:
[DisplayName("Förnamn")]
[Required(ErrorMessage = "Vänligen ange ett förnamn")]
[StringLength(100)]
public string FirstName { get; set; }
[DisplayName("Efternamn")]
[Required(ErrorMessage = "Vänligen ange ett efternamn")]
[StringLength(100)]
public string LastName { get; set; }
[DisplayName("E-post")]
[Required(ErrorMessage = "Vänligen ange epost")]
[StringLength(100)]
[EmailAddress(ErrorMessage = "Ange en korrekt e-postaddress")]
public string Email { get; set; }
[DisplayName("Mobil")]
[DataType(DataType.PhoneNumber)]
public string PhoenNumber { get; set; }
[DataType(DataType.Password)]
[DisplayName("Lösenord")]
public string PassWord { get; set; }
我的看法:
<div class="col-md-4 col-xs-12">
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "credentialsForm" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group createCustomerFormGroup">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control", @name = "FirstName" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group createCustomerFormGroup">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group createCustomerFormGroup">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group createCustomerFormGroup">
@Html.LabelFor(model => model.PassWord, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.PassWord, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PassWord, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group createCustomerFormGroup">
@Html.LabelFor(model => model.PhoenNumber, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.PhoenNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PhoenNumber, "", new { @class = "text-danger" })
</div>
</div>
</div>
}
</div>
表单中的一个字段通过 firebug:
我 运行 这个脚本在 firebug 中,我没有得到任何错误,即使应该有错误,因为一些字段是必需的,但它们没有值:
$("#credentialsForm").validate().numberOfInvalids()
// retunrs 0
$("#credentialsForm").validate().valid()
// returns true
我已经做了几个小时了,我现在快疯了,我错过了什么?
编辑:将问题从 "How to setup MVC 5 unobtrusive validation correctly" 更改为当前标题,因为它比以前的标题更好地描述了我正在寻找的内容。
一段时间后我弄明白了。
我从一个返回局部视图的 AJAX 调用附加了这个表单。
我找到了答案
here
显然,当像这样添加动态数据时,您首先必须去除 'validator' 和 'unobtrusiveValidation' 的形式,然后在形式上调用 $.validator.unobtrusive.parse 函数,如下所示:
var form = $("#main_div").closest("form");
form.removeData('validator');
form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse(form);
可以找到更详细的解释 here
我有关于这个问题的 googeld,我已经检查了我的 web.config、bundleconfig 和我的布局,如下所示: web.config:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
在我 "BundleConfig.cs" 下的 App_Start 文件夹中:
var jqueryBundle = new ScriptBundle("~/bundles/jquery"); jqueryBundle.Include("~/Scripts/jquery-{version}.js"); jqueryBundle.Include("~/Scripts/moment.min.js"); jqueryBundle.Include("~/Scripts/loadingoverlay.js"); jqueryBundle.Include("~/Scripts/fullcalendar.js"); jqueryBundle.Include("~/Scripts/lang-all.js"); jqueryBundle.Transforms.Add(jsTransformer); jqueryBundle.Orderer = nullOrderer; bundles.Add(jqueryBundle);
var jqueryvalBundle = new ScriptBundle("~/bundles/jqueryval"); jqueryvalBundle.Include("~/Scripts/jquery.validate*"); jqueryvalBundle.Include("~/Scripts/jquery.validate.js"); jqueryvalBundle.Include("~/Scripts/jquery.validate.unobtrusive.js"); jqueryvalBundle.Transforms.Add(jsTransformer); jqueryvalBundle.Orderer = nullOrderer; bundles.Add(jqueryvalBundle);
在我的布局页面中:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
Firebug 显示:
到目前为止,一切都包括在内,应该会 运行 顺利进行。
我的模型:
[DisplayName("Förnamn")]
[Required(ErrorMessage = "Vänligen ange ett förnamn")]
[StringLength(100)]
public string FirstName { get; set; }
[DisplayName("Efternamn")]
[Required(ErrorMessage = "Vänligen ange ett efternamn")]
[StringLength(100)]
public string LastName { get; set; }
[DisplayName("E-post")]
[Required(ErrorMessage = "Vänligen ange epost")]
[StringLength(100)]
[EmailAddress(ErrorMessage = "Ange en korrekt e-postaddress")]
public string Email { get; set; }
[DisplayName("Mobil")]
[DataType(DataType.PhoneNumber)]
public string PhoenNumber { get; set; }
[DataType(DataType.Password)]
[DisplayName("Lösenord")]
public string PassWord { get; set; }
我的看法:
<div class="col-md-4 col-xs-12">
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "credentialsForm" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group createCustomerFormGroup">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control", @name = "FirstName" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group createCustomerFormGroup">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group createCustomerFormGroup">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group createCustomerFormGroup">
@Html.LabelFor(model => model.PassWord, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.PassWord, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PassWord, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group createCustomerFormGroup">
@Html.LabelFor(model => model.PhoenNumber, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.PhoenNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PhoenNumber, "", new { @class = "text-danger" })
</div>
</div>
</div>
}
</div>
表单中的一个字段通过 firebug:
我 运行 这个脚本在 firebug 中,我没有得到任何错误,即使应该有错误,因为一些字段是必需的,但它们没有值:
$("#credentialsForm").validate().numberOfInvalids()
// retunrs 0
$("#credentialsForm").validate().valid()
// returns true
我已经做了几个小时了,我现在快疯了,我错过了什么?
编辑:将问题从 "How to setup MVC 5 unobtrusive validation correctly" 更改为当前标题,因为它比以前的标题更好地描述了我正在寻找的内容。
一段时间后我弄明白了。
我从一个返回局部视图的 AJAX 调用附加了这个表单。
我找到了答案 here
显然,当像这样添加动态数据时,您首先必须去除 'validator' 和 'unobtrusiveValidation' 的形式,然后在形式上调用 $.validator.unobtrusive.parse 函数,如下所示:
var form = $("#main_div").closest("form");
form.removeData('validator');
form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse(form);
可以找到更详细的解释 here