本文介绍了从AJAX调用追加表单时,如何正确设置MVC 5非侵入式验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此问题有所了解,并检查了如下所示的web.config,bundleconfig和布局: web.config:

I have googeld about this issue and I have checked my web.config, bundleconfig and my layout which look like this: 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>

在我的App_Start文件夹中的"BundleConfig.cs"下:

In my App_Start folder under "BundleConfig.cs":

        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);

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显示:

Firebug shows:

因此,到目前为止,所有内容都已包含在内,并且应该运行顺畅.

Thus far everything is included and shouold be running smoothly.

我的模特:

   [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>

通过萤火虫形式的字段:

a field in the form via firebug:

我在firebug中运行了此脚本,尽管应该出现错误,但由于出现了某些字段,但它们没有值,所以我没有任何错误:

I run this script in firebug and I get no errors even though there should be errors since some of the fields are requiered but they do not have a value:

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

$("#credentialsForm").validate().valid()
// returns true

我已经呆了几个小时了,现在我疯了,我想念什么?

Ive been at this for hours and im going nuts right now, what am I missing?

将问题从如何正确设置MVC 5非侵入式验证"更改为当前标题,因为它描述了我在寻找比以前的标题更好的东西.

changed the question from "How to setup MVC 5 unobtrusive validation correctly" to it's current title since it described what I was looking for better than the previous title.

推荐答案

一段时间后,我发现了这一点.

After some time I figured this out.

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

I was appending this form from an AJAX call which returned a partial view.

我找到了答案此处

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

apperently when adding dynamic data like this you first have to strip the form of 'validator' and 'unobtrusiveValidation'and then call the $.validator.unobtrusive.parse function on the form, like so:

var form = $("#main_div").closest("form");
form.removeData('validator');
form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse(form);

可以在此处

这篇关于从AJAX调用追加表单时,如何正确设置MVC 5非侵入式验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 17:09