Model Binding in ASP.NET MVC, model binding in asp, model binding in mvc, model binding in asp.net
Hello friends,I hope you all are doing great and having fun with your lives. In today's tutorial, we are gonna have a look at Model Binding in ASP.NET MVC. It's our 13th tutorial in ASP.NET MVC series. Today, we are gonna discuss a new concept in ASP.NET MVC and it's more of a luxury as it automates the job and makes our work easy. Suppose, in your application, you want to create a registration form and thus you need to pass this data from View to your model, so that it could be saved in your database. So, in order to bind this HTTP request data from View to Model, we use Model Binding. Let's have a look at Model Binding in ASP.NET MVC in detail:

Model Binding in ASP.NET MVC

  • Model Binding in ASP.NET MVC is used to bind the data sent by the HTTP request with Model.
  • You must have viewed during some form submission online, that when you click on the submit button then a form of data is sent by the HTTP string.
  • This HTTP data is linked to the Controller's action methods in the form of input parameters.
  • For example, suppose we have an action method "public ActionResult Edit(int id)", here the parameter int id could be used as a model binder.
  • It may happen that this id value is coming from some HTTP request "http://localhost/Student/Create?id=10".
  • So, the model binders get data from View via HTTP request and then pass this value to Controller's action method, which in turn shows up to models.
  • When we create a new form in ASP.NET MVC, then this model binding is automatically done by visual studio.
  • Let's create a new View and have a look at working of Model Binding in ASP.NET MVC.
  • So, in your StudentController.cs file, right click on the action method Create and then click on Add View, as shown in below figure:
Model Binding in ASP.NET MVC, model binding in asp, model binding in mvc, model binding in asp.net
  • When you click on the Add View, it will open a New Window for Creating a View.
  • We have already seen it in How to Create a New View in ASP.NET MVC.
  • So, here's the screenshot for the settings, first I have given this View a name Create, then I have selected Create Template.
  • Finally, I have selected the Model class, which we have created in one of our previous tutorial: Create a New Model in ASP.NET MVC.
Model Binding in ASP.NET MVC, model binding in asp, model binding in mvc, model binding in asp.net
  • After these settings, click the Add Button and a new View for the Create action method will be created.
  • Create.cshtml file will open up in your workspace, as shown in below figure:
Model Binding in ASP.NET MVC, model binding in asp, model binding in mvc, model binding in asp.net
  • If you remember the Student Model, it has four variables as shown in below figure:
Model Binding in ASP.NET MVC, model binding in asp, model binding in mvc, model binding in asp.net
  • So, let's open our newly created Model in the browser.
  • If everything goes fine then you will get similar results:
Model Binding in ASP.NET MVC, model binding in asp, model binding in mvc, model binding in asp.net
  • You can see in the above figure that visual studio has created the same four fields in the View which were present in the Model class.
  • The data we will enter here will be sent to the model and then will be saved in the database.
  • Code for the Model Binding in ASP.ENT MVC has automatically been created, which we have seen in Create.cshtml.
  • Here's the video demonstration of Model Binding in ASP.NET MVC:
So, that was all about Model Binding in ASP.NET MVC. If you got into any trouble in understanding it, then ask in comments. In the next tuorial, we will have a look at Data Validation in ASP.NET MVC. Thanks for reading, Take Care !!! :)