Model Validation In ASP.NET CORE MVC

In this tutorial we will discuss how apply model validation in asp.net core. Will see

      1. How to apply validation on models.
      2. How to display error messages. 
      3. How to display all error messages.
      4. How to customize the error message.
      5. What are Different types of validation?

As of now we have created a form which saves the employee details but we have not added any validation so we want name, class and school name to be mandatory fields If the required values are not provided and the form is submitted we want to display required validation errors and the age field is not mandatory but when use enters value that must be integer.

To make name, class and school name field a required field, we need to apply the Required attribute on the name, class and school name property of the Employee model class. Required attribute is in System.ComponentModel.DataAnnotations namespace.

public class Student
{
///
/// GTechFluent Student Information
/// 

public int ID { get; set; } [Required] public string Name { get; set; } [Required] public string Class { get; set; } public int Age { get; set; } [Required] public string School { get; set; } }

When the form is submitted, the CreateStudent() action method is executed. The model for Create Student Form is Student class. When the form is submitted, model binding maps the posted form values to the respective properties of the Student class

[HttpPost]
public ActionResult CreateStudent(Student student)
{
  _studentRespository.Add(student);  
  return View();
}

As we have the Required attribute on the name, class and school name property of the Student class and if a value for these properties is not present, the validation fails.

We need to use ModelState.IsValid property to check if validation has failed or succeeded If validation has failed we return the same view so the user can provide the required data and resubmit the form.

[HttpPost]
public ActionResult CreateStudent(Student student)
{
  if (ModelState.IsValid)
  {
   _studentRespository.Add(student);
  }
  return View();
}

To display validation errors use asp-validation-for and asp-validation-summary tag helpers. asp-validation-for tag helper displays a validation message for a single property of our model class. asp-validation-summary tag helper displays a summary of validation errors.

To display the validation error associated with the name, class and school name property of the student class use asp-validation-for tag helper on a element under the each property.

To display a summary of all validation errors use asp-validation-summary tag helper on a <div> element as shown below.

Customising Model Validation Error Message

If you notice we are getting the predefined  error message as This field is required but if you want to provide your custom error message like Please provide your first name!

If you want to change the validation error message we can do so using the ErrorMessage property of the Required attribute.

Another User Attribute is Display attribute however is not a validation attribute. It is commonly used for display purposes in the UI.  For example, in the UI by default, the label for Name field displays the text Name, because the property name is Name but if you want the label to display Full Name instead, the we need to use the Display attribute

public class Student
{
///
/// GTechFluent Student Information
/// 

public int ID { get; set; } [Display(Name = “Student Name”)] [Required(ErrorMessage =”Please provide student name!”)] public string Name { get; set; } [Required] public string Class { get; set; } public int Age { get; set; } [Required] public string School { get; set; } }

You can watch our video version of this tutorial with step by step explanation.