Loading

ASP.NET Core

What is Model Binding in ASP.NET Core?. The Complete ASP.NET Core Developer Course 2023 [Videos].

The Model Binding is a mechanism in ASP.NET Core Application that 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.

Model Binding in ASP.NET Core Application

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

What is Model Binding in ASP.NET Core?

The Model Binding is a mechanism in ASP.NET Core Application that 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 that 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 the 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 URLs 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 Video, 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 the method that is going to handle the request. Please decorate the method with the 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 the 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 the 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 Video, we will discuss how to solve this issue when we are working with the database.

At the moment we dont 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.

See All

Comments (260 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Core / Blog

What is ASP.NET Core?

ASP.NET Core is the new version of the ASP.NET web framework mainly targeted to run on .NET Core platform.
27-jan-2022 /16 /260

ASP.NET Core / Blog

What is ASP.NET Core Framework?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /260

ASP.NET Core / Blog

How to Setup ASP.NET Core Environment ?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /260