ASP.NET Core  

Model Binding and Model Validation in ASP.NET Core MVC?

Introduction

In ASP.NET Core MVC, two important features that make working with forms and data easier are Model Binding and Model Validation. These features save developers time and reduce boilerplate code by automatically handling user input and ensuring the data is valid.

🔗 What is Model Binding?

Model Binding is the process in ASP.NET Core MVC that maps data from HTTP requests (like form fields, route values, query strings, and JSON data) to action method parameters or model objects.

Instead of writing custom code to read values from the request and assign them manually, model binding does it automatically.

✅ Example of Model Binding

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

public class StudentController : Controller
{
    [HttpPost]
    public IActionResult Save(Student student)
    {
        // Model binding will map form values to the 'student' object
        return View(student);
    }
}

Here, if a form submits values like Id=1&Name=John&Age=20, ASP.NET Core will automatically bind those values to the Student model.

🛠 Sources of Model Binding

Model binding can take values from:

  • Form fields (e.g., <input> values)

  • Route data (e.g., /student/details/1 → id=1)

  • Query strings (e.g., ?name=John&age=20)

  • JSON body (common in APIs)

✅ What is Model Validation?

Once the model binding maps data to objects, the next step is Model Validation. Model Validation checks if the data meets certain rules before processing it further.

These rules can be defined using Data Annotations or custom validation logic.

✅ Example of Model Validation with Data Annotations

public class Student
{
    public int Id { get; set; }

    [Required(ErrorMessage = "Name is required")]
    [StringLength(50, ErrorMessage = "Name cannot exceed 50 characters")]
    public string Name { get; set; }

    [Range(18, 60, ErrorMessage = "Age must be between 18 and 60")]
    public int Age { get; set; }
}

🛠 How Validation Works

  • When a form is submitted, ASP.NET Core first binds the values to the model.

  • Then it checks all validation attributes.

  • If the model is valid, the action method executes normally.

  • If the model is invalid, the action method can return the same view with validation error messages.

✅ Example Controller with Validation

[HttpPost]
public IActionResult Save(Student student)
{
    if (ModelState.IsValid)
    {
        // Process and save student
        return RedirectToAction("Success");
    }
    else
    {
        // Return the same view with validation errors
        return View(student);
    }
}

âš¡ Combining Model Binding and Validation

Both features work together:

  1. Model Binding → Maps request data to model objects.

  2. Model Validation → Ensures the data is correct before use.

Example flow:

  • A user fills out a registration form.

  • Model binding converts form values into a User object.

  • Model validation checks if all required fields are filled and values are in the correct format.

  • If valid → Save to database.

  • If invalid → Show error messages.

📌 Summary

In ASP.NET Core MVC:

  • Model Binding automatically maps incoming request data (form, query, route, JSON) to controller parameters or model objects.

  • Model Validation checks the mapped data against rules defined using data annotations or custom validators.

Together, these features simplify data handling, reduce manual coding, and help ensure data integrity in your applications.