If you want to:
- Use .NET Model Validation (Data Annotation Attributes)
- Use html5 data attributes instead of non-semantic css classes
You need both: jquery.validate and jquery.validate.unobtrusive
Script Links:
Note. In this example, the js files are located in the "public" folder in the project / site, but if possible, you are using Microsoft from Google CDNs.
<script src="@Url.Content("~/public/js/libs/jquery.validate.min.js")"></script> <script src="@Url.Content("~/public/js/libs/jquery.validate.unobtrusive.min.js")"></script>
Model:
public class PimpModel { [Required] [StringLength(20)] [DisplayName("Pimp Name")] public string PimpName { get; set; } }
Web.config Settings
<appSettings> <add key="ClientValidationEnabled" value="true"/> <add key="UnobtrusiveJavaScriptEnabled" value="true"/> </appSettings>
Show form code
@model SomeProject.Models.PimpModel @{ View.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; Html.EnableUnobtrusiveJavaScript(true); } <h2>Pimp</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> @Html.ValidationMessageFor(model => model.PimpName) <div class="editor-label"> @Html.LabelFor(model => model.PimpName) </div> <div class="editor-field"> @Html.TextBoxFor(model => model.PimpName) </div> <input type="submit" value="Save"></fieldset> }
Html helpers generate html data attributes that are used by an unobtrusive library, an unobtrusive library uses functions in a validation library and all happy days ...
source share