I have an ASP.NET project that automatically wires up client side validation using jQuery.Validate and the unobtrusive wrapper built by ASP.NET.
a) I definitely have the appropriate libraries: jquery.js, jquery.validate.js, & jquery.validate.unobtrusive.js
b) And the MVC rendering engine is definitely turned on (ClientValidationEnabled
& UnobtrusiveJavaScriptEnabled
in the appSettings
section of the web.config)
Here's a trivial example where things are broken:
Model:
<!-- language: lang-cs -->public class Person
{
[Required]
public string Name { get; set; }
}
Controller:
<!-- language: lang-cs -->public ActionResult Edit()
{
Person p = new Person();
return View(p);
}
View:
<!-- language: lang-cs -->@model validation.Models.Person
@using (Html.BeginForm()) {
@Html.ValidationSummary(false)
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
}
This generates the following client side markup:
<!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-html --><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.js"></script>
<form action="/Person" method="post">
<div class="validation-summary-valid" data-valmsg-summary="true">
<ul><li style="display:none"></li></ul>
</div>
<label for="Name">Name</label>
<input data-val="true" data-val-required="The Name field is required." id="Name" name="Name" type="text" value="" />
<input type="submit" value="Save" />
</form>
<!-- end snippet -->
When run it will perform the client side validation, noting that some form elements are invalid, but then also post back to the server.
Why is it not preventing postback on a form with an invalid state?