ValidationExtensions.ValidationMessage Method (HtmlHelper, String, IDictionary<String, Object>)

Displays a validation message if an error exists for the specified field in the ModelStateDictionary object.

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

Syntax

'Declaration
<ExtensionAttribute> _
Public Shared Function ValidationMessage ( _
    htmlHelper As HtmlHelper, _
    modelName As String, _
    htmlAttributes As IDictionary(Of String, Object) _
) As MvcHtmlString
'Usage
Dim htmlHelper As HtmlHelper 
Dim modelName As String 
Dim htmlAttributes As IDictionary(Of String, Object)
Dim returnValue As MvcHtmlString 

returnValue = htmlHelper.ValidationMessage(modelName, _
    htmlAttributes)
public static MvcHtmlString ValidationMessage(
    this HtmlHelper htmlHelper,
    string modelName,
    IDictionary<string, Object> htmlAttributes
)
[ExtensionAttribute]
public:
static MvcHtmlString^ ValidationMessage(
    HtmlHelper^ htmlHelper, 
    String^ modelName, 
    IDictionary<String^, Object^>^ htmlAttributes
)
static member ValidationMessage : 
        htmlHelper:HtmlHelper * 
        modelName:string * 
        htmlAttributes:IDictionary<string, Object> -> MvcHtmlString
public static function ValidationMessage(
    htmlHelper : HtmlHelper, 
    modelName : String, 
    htmlAttributes : IDictionary<String, Object>
) : MvcHtmlString

Parameters

  • modelName
    Type: System.String

    The name of the property or model object that is being validated.

Return Value

Type: System.Web.Mvc.MvcHtmlString
If the property or object is valid, an empty string; otherwise, a span element that contains an error message.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type HtmlHelper. When you use instance method syntax to call this method, omit the first parameter. For more information, see https://msdn.microsoft.com/en-us/library/bb384936(v=vs.108) or https://msdn.microsoft.com/en-us/library/bb383977(v=vs.108).

Remarks

The ValidationMessage method renders a validation message if the specified field contains invalid input.

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.

See Also

Reference

ValidationExtensions Class

ValidationMessage Overload

System.Web.Mvc.Html Namespace