ASP.NET MVC: ValidationMessageFor

The Html.ValidationMessageFor() is a strongly typed extension method. It displays a validation message if an error exists for the specified field in the ModelStateDictionary object.

Signature:
MvcHtmlString ValidateMessageFor(Expression<Func<dynamic,TProperty>> expression, string validationMessage, object htmlAttributes)

Visit MSDN to know all the overloads of ValidationMessageFor() method.

The following Student model class with the Required validation attribute on the StudentName.

Example: Student Model
public class Student
{
    public int StudentId { get; set; }
    [Required]
    public string StudentName { get; set; }
    public int Age { get; set; }
}

The following view uses the ValidationMessageFor() method for the StudentName.

Example: ValidationMessageFor
@model Student  
    
@Html.EditorFor(m => m.StudentName) <br />
@Html.ValidationMessageFor(m => m.StudentName, "", new { @class = "text-danger" })

In the above example, the first parameter in the ValidationMessageFor() method is a lambda expression to specify a property for which we want to show an error message. The second parameter is for custom error message if any, and the third parameter is for HTML attributes such as CSS, style, etc.

The above code will generate the following HTML when you run it.

Html Result:
<input id="StudentName" 
        name="StudentName" 
        type="text" 
        value="" />

<span class="field-validation-valid text-danger" 
        data-valmsg-for="StudentName" 
        data-valmsg-replace="true">
</span>

Now, when the user submits a form without entering a StudentName then ASP.NET MVC uses the data- attribute of HTML5 for the validation and the default validation message will be injected when validation error occurs, as shown below.

Html with Validation message:
<span class="field-validation-error text-danger" 
            data-valmsg-for="StudentName" 
            data-valmsg-replace="true">The StudentName field is required.</span>

The error message will appear as the image shown below.

Custom Error Message

You can display custom error messages instead of the default error message as above. You can provide a custom error message either in the data annotation attribute or in the ValidationMessageFor() method.

Use the ErrorMessage parameter of the data annotation attribute to provide your own custom error message, as shown below.

Example: Custom error message in the Model
public class Student
{
    public int StudentId { get; set; }
    [Required(ErrorMessage="Please enter student name.")]
    public string StudentName { get; set; }
    public int Age { get; set; }
}

You can also specify a message as a second parameter in the ValidationMessage() method, as shown below.

Example: Custom error message
@model Student  
    
@Html.Editor("StudentName") <br />
@Html.ValidationMessageFor(m => m.StudentName, "Please enter student name.", new { @class = "text-danger" })

It is recommended to use ValidationMessageFor() than ValidationMessage() because it is strongly typed and so performs fast and less error pron.