Model Binding in ASP.NET Core

Model Binding in ASP.NET Core Application

In this article, I am going to discuss Model Binding in ASP.NET Core Application. Please read our previous article where we discussed how to create a Form in ASP.NET Core Application using Form Tag Helpers. Please read our previous article as we are going to work with the same application that we worked in our previous article.

What is Model Binding in ASP.NET Core?

The Model Binding is a mechanism in ASP.NET Core Application which extracts the data from an HTTP request and provides them to the controller action method parameters.

The action method parameters may be simple types like integers, strings, etc. or complex types such as Student, Order, Product, etc.

How does the Model Binding works in ASP.NET Core?

As we already discussed, it is our controller action method which is going to handle the incoming HTTP Request in ASP.NET Core MVC Application.

Example using Route Data:

Let us understand this with an example. When we want to view the details of a student whose id is 101, then we generally issue a GET request to the following URL.

http://localhost:52191/home/details/101

Our application default route template ({controller=Home}/{action=Index}/{Id?}) routes the above GET request to the Details(int Id) action method of the HomeController. The following image shows the Details action method of the Home Controller.

Model Binding in ASP.NET Core

So, the value 101 in the request URL is mapped to the Id parameter of the Details(int Id) action method of Home Controller. The MVC Framework will automatically bind the data in the request to the action method parameters by name.

If you notice, the parameter name in the default route template is “Id” and the parameter name of the Details(int Id) action method is also “Id”. So the value 101 in the URL (http://localhost:52191/home/details/101) is mapped to the Id parameter of the Details(int Id) action method.

Example using Query String:

Let us understand this with an example. First, modify the Details Action method as shown below. As you can see we made two changes here. First, we change the return type of the action method to string. Secondly, the Details method now taking two parameters.

How does the Model Binding works in ASP.NET Core

Now issue a Get Request as shown below.

http://localhost:52191/home/details/101?name=dotnet

The above GET request will handle by the Details action method and it will map the value 101 to the Id parameter and the value dotnet will be mapped to the name parameter of the Details action method.

HTTP Request Data Sources:

ASP.NET Core MVC uses three primary data sources to map the HTTP requests data to the action method parameter in the following order:

  1. Form values: Values in the FORM in HTTP POST requests.
  2. Route values: Values provided by the Routing system.
  3. Query string: Values found in the URL’s query string (e.g. after ? character).
Model Binding in ASP.NET Core with Complex Type:

The Model Binding in ASP.NET Core Application also works with complex types like Customer, Student, Order, Product, etc. Let us understand this with an example. In the previous article, we created the following Create Student form.

Model Binding in ASP.NET Core with Complex Type

Add the following Create method to the Home Controller. When the above form is posted, this is method which is going to handle the request. Please decorate the method with HttpPost attribute.

[HttpPost]
public ActionResult Create(Student student)
{
    student.StudentId = listStudents.Max(x => x.StudentId) + 1;
    listStudents.Add(student);
    return View("Details", student);
}
How does it work?

When the form is submitted, the values in the form are mapped to the Student object parameter to the Post Create action method. The Model binder in asp.net core application binds the posted form values to the properties of the Student 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 Studnet object. Similarly, the value in the Branch input element will be mapped to the Branch property of the Student object. This is going to be same for the rest of the properties like Email and Gender.

Note: At the moment if you navigate to list view, then you will not find the newly created student data. In a later article, we will discuss how to solve this issue when we are working with the database.

At the moment we don’t have any validation on the Create Student Form. So, if we submit the form without filling any of the form fields, then we will end up creating a new student with empty data.

In the next article, I am going to discuss Model Validation in ASP.NET Core Application. Here, in this article, I try to explain Model Binding in ASP.NET Core Application.

1 thought on “Model Binding in ASP.NET Core”

Leave a Reply

Your email address will not be published. Required fields are marked *