我在使用 nuget 万无一失的包进行客户端验证时遇到问题。
我的模特
public class Item {
public int Id { get; set; }
[Required]
public double Quantity { get; set; }
[RequiredIfNotEmpty("Quantity")]
public string LocationCode { get; set; }
public IEnumerable<SelectListItem> LocationList { get; set; }
}
在我看来我有
@model IList<MyApp.Models.Item>
<!-- some HTML -->
@for(int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.DropDownListFor(p => p[i].LocationCode, Model[i].LocationList, string.Empty, new { @class = "form-control" })
@Html.ValidationMessageFor(p => p[i].LocationCode)
</td>
<td>
@Html.TextBoxFor(p => p[i].Quantity, new { @class = "form-control"})
@Html.ValidationMessageFor(p => p[i].Quantity)
</td>
</tr>
}
在我的 _Layout.cshtml 中,我包含以下捆绑包:
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/jqueryval")
@RenderSection("scripts", required: false)
我的 jqueryval 包看起来像这样:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*",
//"~/Scripts/MvcFoolproofJQueryValidation.min.js",
"~/Scripts/mvcfoolproof.unobtrusive.min.js"
//"~/Scripts/MvcFoolproofValidation.min.js"
));
在我的 web.config 中,我有:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
我已经尝试过使用 mvcfoolproof js 文件的不同组合,但我根本无法让客户端验证正常工作。对于数量文本框,它正在工作。请注意,服务器端正在工作。