Chapter 16B. Validation Helpers in MVC

Your web application must verify that all of the data entered by users is valid. This once formidable task has been made easier by a special class of HTML helpers that support validating input from an HTML form. Most of these special HTML helpers are members of the ValidationExtensions class. In this lesson I show you how to use the validation helpers provided by the ASP.NET MVC framework.

VALIDATION HELPERS

These are the validation helpers that are included as part of the ASP.NET MVC framework:

  • Html.EnableClientValidation() — This method enables client-side validation.

  • Html.Validate() — This method retrieves the validation metadata for the given model and applies each rule.

  • Html.ValidateFor() — This method retrieves the validation metadata for the given strongly-typed object and applies each rule.

  • Html.ValidationMessage() — This method displays the error message if an error exists for a given field.

  • Html.ValidationMessageFor() — This method displays all of the error messages associated with a strongly-typed object.

  • Html.ValidationSummary() — This method returns a bulleted list of all of the errors in the ModelStateDictionary object.

    Warning

    To enable client-side validation, you must add this line of code

    <% Html.EnableClientValidation(); %>

    before

    <% using (Html.BeginForm()) {%>

    and your page must refer to the following three files from the Scripts folder:

    <script src="../../Scripts/jquery-1.3.2.js" type="text/javascript" /> <script src="../../Scripts/jquery.validate.js" ...

Get ASP.NET 4 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.