服务器端验证工作正常(一旦我提交表单) 但客户端不起作用。
在我的布局中:
@Scripts.Render("~/bundles/jqueryval")
捆绑:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery.validate.js",
"~/Scripts/jquery.validate.unobtrusive.js"));
在我的配置文件中:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
我在我的模型和 MVC 5 中使用数据注释
虚拟机:
public class LocationWorkingHoursVM
{
public int LocationID { get; set; }
public string Name { get; set; }
[Range(0,1)]
public decimal?[] Hours { get; set; }
public string BankHolidays { get; set; }
}
它呈现为:
<input class="form-control" data-val="true" data-val-number="The field Nullable`1 must be a number." id="Hours_0_" name="Hours[0]" type="text" value="8.0" />
<span class="field-validation-valid" data-valmsg-for="Hours" data-valmsg-replace="true"></span>
我错过了什么?
最佳答案
这是因为我使用自己的 EditorFor 助手模板。然而我把它定义为
TextBoxFor(r=>r.model, new { @class= "foo")})
是否可以使用我的模板使其工作?
关于c# - 客户端不显眼的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24576625/