如何在从AJAX调用中附加表单时正确设置MVC 5不显眼的验证?

Thu*_*3eR 11 jquery unobtrusive-validation asp.net-mvc-5

我有关于这个问题的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>
Run Code Online (Sandbox Code Playgroud)

在"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);
Run Code Online (Sandbox Code Playgroud)

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")
Run Code Online (Sandbox Code Playgroud)

萤火虫表明:

在此输入图像描述

到目前为止,一切都包括在内,并且应该顺利运行.

我的模特:

   [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; }
Run Code Online (Sandbox Code Playgroud)

我的看法:

<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>
Run Code Online (Sandbox Code Playgroud)

通过firebug形式的字段: 在此输入图像描述

我在firebug中运行此脚本,即使存在错误也没有错误,因为某些字段是必需的,但它们没有值:

$("#credentialsForm").validate().numberOfInvalids()
// retunrs 0

$("#credentialsForm").validate().valid()
// returns true
Run Code Online (Sandbox Code Playgroud)

我已经在这几个小时了,我现在疯了,我错过了什么?

编辑:将问题从"如何正确设置MVC 5不引人注意的验证"更改为它的当前标题,因为它描述了我想要的比以前的标题更好.

Thu*_*3eR 12

过了一段时间,我想出来了.

我正在从AJAX调用中附加此表单,该调用返回了部分视图.

我在这里找到了答案

在添加这样的动态数据时,首先必须删除'validator'和'unobtrusiveValidation'的形式,然后在表单上调用$ .validator.unobtrusive.parse函数,如下所示:

var form = $("#main_div").closest("form");
form.removeData('validator');
form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse(form);
Run Code Online (Sandbox Code Playgroud)

可以在此处找到更详细的表达方式