gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:15:11 28 4
gpt4 key购买 nike

我已经向 googeld 询问了这个问题,并且检查了我的 web.config、bundleconfig 和我的布局,如下所示: 网络配置:

<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”下:

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

enter image description here

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

我的模型:

   [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 表单中的字段: enter image description here

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

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

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

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

编辑:将问题从“如何正确设置 MVC 5 不引人注目的验证”更改为当前标题,因为它比以前的标题更好地描述了我正在寻找的内容。

最佳答案

过了一段时间我就明白了这一点。

我从返回部分 View 的 AJAX 调用附加此表单。

我找到了答案 here

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

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

更详细的说明可以参见 here

关于jquery - 从 AJAX 调用附加表单时如何正确设置 MVC 5 不显眼的验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33107674/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com