Model Binding In ASP.NET CORE MVC 

In this tutorial we will discuss Model Binding in ASP.NET Core with examples. Understand the binding to primitive type and binding to complex types.

Model binding maps data in an HTTP request to controller action method parameters. The action parameters may be simple types such as integers, strings, etc or complex types like Customer, Employee, Order etc.

Model binding is great, because without it we have to write lot of custom code to map request data to action method parameters which is not only tedious but also error prone.

The model binding refers to converting the HTTP request data (from the query string or form collection) to an action method parameters. These parameters can be of primitive type or complex type.

Binding to Primitive Type:

When an HTTP request arrives at our MVC application it is the Controller action method that handles the incoming request. Let’s say we want to view student details whose ID is 1. For this we issue a GET request to the following URL

https://localhost:44374/student/details/1

http://localhost:48118/home/details/2

Our application default route template ({controller=Home}/{action=Index}/{id?}) routes this request to Details(int? id) action method of the StudentController.

The id value 1 in the request URL is mapped to the id parameter of the details(int? id) action method. MVC will bind the data in the request to the action parameters by name. Notice, in the above example, the parameter name in the default route template is “id” and the parameter name on the Details(int? id) action method is also id. So the value 2 in the URL (http://localhost:44374/student/details/1) is mapped to the id parameter on the Details(int? id) action method.

HTTP Request Data

To bind the request data to the controller action method parameters, model binding looks for data in the HTTP request in the following places in the order specified below.

      • Form values: HTTP POST requests form values
      • Route values: Routing values
      • Query strings:  Query string parameter values after ?.

Binding to Complex Type:

Model Binding also works with complex types such as Student, School, Employee etc. Consider the following “Create Student Form”

When the above form is posted to the server, the values in the form are mapped to the Student object parameter of the following Create() action method.

Model binder in asp.net core binds the posted form values to the properties of the Employee object that is passed as a parameter to the Create() action method.

The value in the input element that has the name attribute set to “Name” is mapped to the Name property of the Student object

Similarly value in the input element with name “School” is mapped to School property of the Student objects and other properties of student object.

We need to make sure we must give HTTP gets and HTTP post into the action. This is because asp.net core does not know which action method to execute. We want the first Create() action method to respond to GET request and the second Create() action method to respond to the POST request. To tell this to asp.net core decorate the Create() action methods with HttpGet and HttpPost attributes as shown below.

[HttpGet]
public ActionResult CreateStudent()
{
    return View();
}

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

As of now our Create Student Form does not have any validation in place. If we submit the form without filling any of the form fields, we will end up creating a new employee whose name and email fields are null. We will discuss form validation in our next video.

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