简体   繁体   中英

Unobtrusive Client Validation in ASP.NET MVC Still Submit Form

I'm using ASP MVC 4 and I have both ClientValidationEnabled and UnobtrusiveJavaScriptEnabled appSettings turn on but when I submit a form with unobtrusive validation data attributes it get submitted even when it is invalid. The validation message appear but the form still get submitted.

JavaScript is enabled in my browser off course.

Make sure, you have following libraries linked

<script src="~/Scripts/jquery.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

Check your configuration. These two should look as follows:

<appSettings>
    <add key="ClientValidationEnabled" value="true"/> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
</appSettings>

Don't forget to add:

@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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM