2

我正在使用 ASP MVC 4,并且我同时打开了 ClientValidationEnabled 和 UnobtrusiveJavaScriptEnabled appSettings,但是当我提交具有不显眼的验证数据属性的表单时,即使它无效,它也会被提交。出现验证消息,但仍会提交表单。

JavaScript 在我的浏览器中启用了。

4

2 回答 2

7

确保您链接了以下库

<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)
于 2013-07-08T11:58:38.633 回答
5

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 
于 2013-07-08T12:13:46.587 回答