我正在使用 ASP MVC 4,并且我同时打开了 ClientValidationEnabled 和 UnobtrusiveJavaScriptEnabled appSettings,但是当我提交具有不显眼的验证数据属性的表单时,即使它无效,它也会被提交。出现验证消息,但仍会提交表单。
JavaScript 在我的浏览器中启用了。
我正在使用 ASP MVC 4,并且我同时打开了 ClientValidationEnabled 和 UnobtrusiveJavaScriptEnabled appSettings,但是当我提交具有不显眼的验证数据属性的表单时,即使它无效,它也会被提交。出现验证消息,但仍会提交表单。
JavaScript 在我的浏览器中启用了。
确保您链接了以下库
<script src="~/Scripts/jquery.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
检查您的配置。这两个应该如下所示:
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
不要忘记添加:
@Html.ValidationMessageFor(m => m.FieldToValidate)
For MVC4 in your master page add below line
@Scripts.Render("~/bundles/jqueryval")
and make sure that in bundles.config below lines are present.
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
once you have added jquery.unobtrusive you will encounter below error.
the live() function is not available anymore in jquery 1.9, which has been deprecated.
TypeError: $(…).live is not a function
in order to resolve this you can manyally replace .live with .On or just add beta release from nuget
Install-Package Microsoft.jQuery.Unobtrusive.Ajax -Pre