Export (0) Print
Expand All

ValidationExtensions Class

Provides support for validating the input from an HTML form.

System.Object
  System.Web.Mvc.Html.ValidationExtensions

Namespace:  System.Web.Mvc.Html
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

public final class ValidationExtensions

The ValidationExtensions type exposes the following members.

  NameDescription
Public propertyStatic memberResourceClassKeyGets or sets the name of the resource file (class key) that contains localized string values.
Top

  NameDescription
Public methodStatic memberValidateRetrieves the validation metadata for the specified model and applies each rule to the data field.
Public methodStatic memberValidateFor(TModel, TProperty)Retrieves the validation metadata for the specified model and applies each rule to the data field.
Public methodStatic memberValidationMessage(HtmlHelper, String)Displays a validation message if an error exists for the specified field in the ModelStateDictionary object.
Public methodStatic memberValidationMessage(HtmlHelper, String, IDictionary(String, Object))Displays a validation message if an error exists for the specified field in the ModelStateDictionary object.
Public methodStatic memberValidationMessage(HtmlHelper, String, Object)Displays a validation message if an error exists for the specified field in the ModelStateDictionary object.
Public methodStatic memberValidationMessage(HtmlHelper, String, String)Displays a validation message if an error exists for the specified field in the ModelStateDictionary object.
Public methodStatic memberValidationMessage(HtmlHelper, String, String, IDictionary(String, Object))Displays a validation message if an error exists for the specified field in the ModelStateDictionary object.
Public methodStatic memberValidationMessage(HtmlHelper, String, String, Object)Displays a validation message if an error exists for the specified field in the ModelStateDictionary object.
Public methodStatic memberValidationMessageFor(TModel, TProperty)(HtmlHelper(TModel), Expression(Func(TModel, TProperty)))Returns the HTML markup for a validation-error message for each data field that is represented by the specified expression.
Public methodStatic memberValidationMessageFor(TModel, TProperty)(HtmlHelper(TModel), Expression(Func(TModel, TProperty)), String)Returns the HTML markup for a validation-error message for each data field that is represented by the specified expression, using the specified message.
Public methodStatic memberValidationMessageFor(TModel, TProperty)(HtmlHelper(TModel), Expression(Func(TModel, TProperty)), String, IDictionary(String, Object))Returns the HTML markup for a validation-error message for each data field that is represented by the specified expression, using the specified message and HTML attributes.
Public methodStatic memberValidationMessageFor(TModel, TProperty)(HtmlHelper(TModel), Expression(Func(TModel, TProperty)), String, Object)Returns the HTML markup for a validation-error message for each data field that is represented by the specified expression, using the specified message and HTML attributes.
Public methodStatic memberValidationSummary(HtmlHelper)Returns an unordered list (ul element) of validation messages that are in the ModelStateDictionary object.
Public methodStatic memberValidationSummary(HtmlHelper, Boolean)Returns an unordered list (ul element) of validation messages that are in the ModelStateDictionary object and optionally displays only model-level errors.
Public methodStatic memberValidationSummary(HtmlHelper, String)Returns an unordered list (ul element) of validation messages that are in the ModelStateDictionary object.
Public methodStatic memberValidationSummary(HtmlHelper, Boolean, String)Returns an unordered list (ul element) of validation messages that are in the ModelStateDictionary object and optionally displays only model-level errors.
Public methodStatic memberValidationSummary(HtmlHelper, String, IDictionary(String, Object))Returns an unordered list (ul element) of validation messages that are in the ModelStateDictionary object.
Public methodStatic memberValidationSummary(HtmlHelper, String, Object)Returns an unordered list (ul element) of validation messages in the ModelStateDictionary object.
Public methodStatic memberValidationSummary(HtmlHelper, Boolean, String, IDictionary(String, Object))Returns an unordered list (ul element) of validation messages that are in the ModelStateDictionary object and optionally displays only model-level errors.
Public methodStatic memberValidationSummary(HtmlHelper, Boolean, String, Object)Returns an unordered list (ul element) of validation messages that are in the ModelStateDictionary object and optionally displays only model-level errors.
Top

The ValidationExtensions class contains methods that extend the HtmlHelper class. The ValidationMessage method renders a validation message if the specified field contains invalid input. The ValidationSummary method displays a list of all validation messages on the page. These methods require the view to be strongly typed.

If the DefaultModelBinder instance cannot bind a form-field value to the model, the binder adds an error to the ModelState object. When the view is rendered, the validation messages and validation summary are displayed based on the Errors property of the ModelState object.

You can control the appearance of the validation messages and validation summary by modifying the following cascading style sheet (CSS) classes in the Site.css file:

  • input-validation-error. This rule is applied to the input element that is rendered by the TextBox helper method.

  • field-validation-error. This rule is applied to the span element that is rendered by the ValidationMessage method.

  • validation-summary-error. This rule is applies to the ul element that is rendered by the ValidationMessage method.

The following example shows one way to use the ValidationSummary and ValidationMessage methods in MVC applications. This example defines a model class named Person. The Person class establishes the basic member requirements. For example, Name must be a string and Age must be an integer.

The Create view enables the user to create a new Person object. A call to the ValidationSummary method is placed in the view ahead of the entry form. If an invalid value is submitted, the summary displays an error message. A call is made to the ValidationMessage method following each form field. If the associated form field contains an invalid value, the field is marked with an asterisk (*).

When the form is submitted, the Create action method handles the request. In this example, the Create method checks validation errors, such as whether the phone number and email address match the regular expressions that define a valid entry. If an error is found, an error message is added to the ModelState object and the Create view is re-displayed showing the errors.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft