Asp.net mvc 5 所需的现场验证未启动
我正在构建一个MVC5表单,需要对必填字段进行验证。我有几个文本控件需要验证。我尝试在模型上添加Asp.net mvc 5 所需的现场验证未启动,asp.net-mvc-5,Asp.net Mvc 5,我正在构建一个MVC5表单,需要对必填字段进行验证。我有几个文本控件需要验证。我尝试在模型上添加[Required]属性,并在客户端的htmlattributes中添加Required关键字,但验证从未启动。有人能告诉我如何处理这个验证吗 <div class="form-group"> @Html.LabelFor(model => model.CustomerNumber, htmlAttributes: new { @class = "control-label
[Required]
属性,并在客户端的htmlattributes
中添加Required
关键字,但验证从未启动。有人能告诉我如何处理这个验证吗
<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>
@LabelFor(model=>model.CustomerNumber,htmlAttributes:new{@class=“controllabel col-md-4”})
@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”})
@Html.ValidationMessageFor(model=>model.CustomerNumber,“,new{@class=“text danger”})
[必需]
或其他验证器属性\u Layout.cshtml
文件中...
<body>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
</body>
</html>
。。。
@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/jqueryval”)
@Scripts.Render(“~/bundles/bootstrap”)
您只需要属性上的[必需]
属性。您是否包含了客户端验证所需的脚本?并且在使用HtmlHelper
方法时,切勿设置value
属性。在将模型传递到视图之前,在控制器中设置CustomerNumber
的值。我已在viewmodel的CustomerNumber字段中添加了必需的属性。尚未添加任何客户端脚本。如果你需要jquery-{version}.js
,jquery.validate.js
和jquery.validate.unobtrusive.js
在你的视图或布局中,我确实参考了这些文件