我正在构建一个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>
答案 0 :(得分:0)
[Required]
或其他验证器属性添加到“模型”字段中。_Layout.cshtml
文件中。示例:
...
<body>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
</body>
</html>