• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Saturday, September 5, 2020

Model Binding in ASP.NET Core

 Admin     September 05, 2020     .Net, .Net Core, .Net Core MVC, Asp.Net, C#     1 comment   

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 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 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 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 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 the 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.

Summary:
I hope this post will be helpful to understand the concept of Model Binding in ASP.NET Core
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

1 comment:

  1. Sapphire SolutionsNovember 24, 2021 at 12:30 PM

    Very informative post..

    About - Model Binding in Asp.Net Core

    Top 6 Practical Reasons to Learn C# Programming Language in 2021

    C# development services

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • ASP.NET State Management
    State management is a technique or way to maintain / store the state of an Asp.Net controls, web page information, object/data, and user in ...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • HTTP Client Message Handler in Web API
    In this article, I am going to discuss HTTP Client Message Handler in Web API with real-time examples. As we already discussed in HTTP Mes...
  • ASP.NET Web API Basic Authentication
    In this article, I am going to discuss how to implement the ASP.NET Web API Basic Authentication step by step with an example. Please read...
  • Views in ASP.NET Core MVC
    In this article, I am going to discuss Views in the ASP.NET Core MVC application. Please read our previous article before proceeding to th...
  • How to find the angle between hour and minute hands of a clock at any given time in C#
    In this article, I am going to discuss how to find the angle between the hour and minute hands of a clock at any given time in C# with an ...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape