简体   繁体   English

MVC 5万无一失的客户端验证

[英]MVC 5 Foolproof client-side validation

I'm having trouble with client-side validation with the nuget foolproof package. 我在使用nuget万无一失的软件包进行客户端验证时遇到了麻烦。

My Model 我的模特

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

In my View I have 我认为我有

    @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>
    }

In my _Layout.cshtml I have the following bundles included: 在_Layout.cshtml中,包含以下捆绑软件:

@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/jqueryval")
@RenderSection("scripts", required: false)

And my jqueryval bundle looks like this: 我的jqueryval包看起来像这样:

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*",
                        //"~/Scripts/MvcFoolproofJQueryValidation.min.js",
                        "~/Scripts/mvcfoolproof.unobtrusive.min.js"
                        //"~/Scripts/MvcFoolproofValidation.min.js"
                        ));

In my web.config I have: 在我的web.config中,我有:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

I've tried different combinations with mvcfoolproof js files, but I simply can't get the client-side validation to work. 我已经尝试过使用mvcfoolproof js文件进行不同的组合,但是我根本无法使客户端验证正常工作。 For the Quantity textbox it is working. 对于数量文本框,它正在工作。 Note that Server-side IS working. 请注意,服务器端IS正在运行。

Try to change model from IList to MyApp.Models.Item and pass only one item from controller to view. 尝试将模型从IList更改为MyApp.Models.Item,并将仅一项从控制器传递到视图。 Client validation should work then. 然后,客户端验证应该起作用。 I think that @for() loop is the problem - you have multiple "Quantity" fields and razor can't choose correct one. 我认为@for()循环是个问题-您有多个“数量”字段,而剃刀无法选择正确的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM