必需的字段验证未触发

时间:2017-04-19 08:22:59

标签: asp.net-mvc-5

我正在构建一个MVC5表单,需要对必填字段进行验证。我有几个需要验证的文本控件。我尝试在模型上添加[Required]属性,并在客户端的required中添加htmlattributes关键字,但验证永远不会触发。有人可以告诉我如何处理这个验证

<div class="form-group">
     @Html.LabelFor(model => model.CustomerNumber, htmlAttributes: new { @class = "control-label col-md-4" })
     <div class="col-md-8">
        <div class="editor-field">
            @Html.EditorFor(model => model.CustomerNumber, new { htmlAttributes = new { Value = Model.CustomerNumber == 0 ? "" : Model.CustomerNumber.ToString(), @class = "form-control", style = "width:100%", @readonly = "readonly", required = "required" } })
        </div>
        @Html.ValidationMessageFor(model => model.CustomerNumber, "", new { @class = "text-danger" })
     </div>
</div>

1 个答案:

答案 0 :(得分:0)

  1. [Required]或其他验证器属性添加到“模型”字段中。
  2. 将jQuery验证库添加到_Layout.cshtml文件中。
  3. 示例:

    ...
    <body>
        @Scripts.Render("~/bundles/jquery")
           @Scripts.Render("~/bundles/jqueryval")
        @Scripts.Render("~/bundles/bootstrap")
    </body>
    </html>