如何在从AJAX调用中附加表单时正确设置MVC 5不显眼的validation?
我有关于这个问题的googeld,我检查了我的web.config,bundleconfig和我的布局,如下所示:web.config:
在“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( “〜/脚本/ jquery.validate *”); jqueryvalBundle.Include( “〜/脚本/ jquery.validate.js”); jqueryvalBundle.Include( “〜/脚本/ 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")
萤火虫表明:
到目前为止,一切都包括在内,并且应该顺利运行。
我的模特:
[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; }
我的看法:
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "credentialsForm" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-3" }) @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control", @name = "FirstName" } }) @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" }) @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-3" }) @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" }) @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-3" }) @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) @Html.LabelFor(model => model.PassWord, htmlAttributes: new { @class = "control-label col-md-3" }) @Html.EditorFor(model => model.PassWord, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.PassWord, "", new { @class = "text-danger" }) @Html.LabelFor(model => model.PhoenNumber, htmlAttributes: new { @class = "control-label col-md-3" }) @Html.EditorFor(model => model.PhoenNumber, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.PhoenNumber, "", new { @class = "text-danger" }) }
通过firebugforms的字段:
我在firebug中运行此脚本,即使存在错误也没有错误,因为某些字段已被取消但是它们没有值:
$("#credentialsForm").validate().numberOfInvalids() // retunrs 0 $("#credentialsForm").validate().valid() // returns true
我已经在这几个小时了,现在我疯了,我错过了什么?
编辑:将问题从“如何正确设置MVC 5不引人注意的validation”更改为它的当前标题,因为它描述了我想要的比以前的标题更好。
过了一段时间我想出来了。
我在AJAX调用中附加了此表单,该调用返回了部分视图。
我在这里找到了答案
在添加这样的动态数据时,首先必须删除’validator’和’unobtrusiveValidation’的forms,然后在表单上调用$ .validator.unobtrusive.parse函数,如下所示:
var form = $("#main_div").closest("form"); form.removeData('validator'); form.removeData('unobtrusiveValidation'); $.validator.unobtrusive.parse(form);
可以在此处找到更详细的表达方式
- DateTimePicker的validation无法正常工作
- JQuery UI自动完成ASP.NET MVC5渲染列表
- Javascript变量存储Garbage而不是输入框中的值?
- “0x800a1391 – JavaScript运行时错误:’jQuery’未定义”错误
- 使用Jquery Datepicker的DateFormat错误
- Foolproof RequiredIfTrue不适用于MVC5?
- 带字幕的多图像文件上传
- 为什么Asp.Net MVC 5将@ Scripts.Render(“〜/ bundles / jquery”)放在_Layout.cshtml的底部?
- Ajax json到MVC5控制器:传递包含集合的对象