Introduction To Model Binding in ASP.Net MVC: Part 1

Introduction

Today we'll learn about Model Binding in MVC applications. Model Binding allows you to map and bind the HTTP request data with a model. If you want to work with the form data, Model Binding makes it easier because the requested data is submitted automatically into a data model that we specify. The Default Binder is used to do this, if we go deep intp this.

In that context, you'll learn here the model biding working with the various types of models such as:

  • No Binding
  • Binding with Simple Types
  • Binding with Class Types
  • Binding with Class Properties

Before Getting Started

You must be familiar with the knowledge of MVC or you can refer to Getting Started with MVC. Here, I am developing an application on Visual Studio 2013 using MVC 5 Project Template. So, start with the following points:

  • Open Visual Studio 2013 and click on "New Project"
  • Select the ASP.NET Web Application and enter the name to it
  • Select the MVC project template to develop the application

No Binding

In this section we'll see how form data is to be used without no Model Binding. So, follow the procedure below.

Step 1: Scaffold a new MVC empty controller named "ModelBinding".

Scaffolding MVC Empty Controller

Step 2: Modify the Index() method with the following code:

  1. public ActionResult Index()  
  2. {  
  3.     if (Request.Form.Count > 0)  
  4.     {  
  5.         string ID= Request.Form["StudentID"];  
  6.         string FirstName = Request.Form["StudentFirstName"];  
  7.         ViewBag.Message = "Your Student retrieved successfully with an ID" + " " +ID+ "!!";  
  8.     }  
  9.     return View();  
  10. } 

Step 3: Generate the view named Index and replace the code with the following code:

  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4. <h2>Index</h2>  
  5. @using (Html.BeginForm())  
  6. {  
  7.     <div class="form-horizontal">  
  8.         <h4>Enter Details</h4>  
  9.         <hr />  
  10.         <div class="form-group">  
  11.             @Html.Label("ID:"new { @class = "col-md-2 control-label" })  
  12.             <div class="col-md-10">  
  13.                 @Html.TextBox("StudentID")  
  14.             </div>  
  15.         </div>  
  16.         <div class="form-group">  
  17.             @Html.Label("Name:"new { @class = "col-md-2 control-label" })  
  18.             <div class="col-md-10">  
  19.                 @Html.TextBox("StudentFirstName")  
  20.             </div>  
  21.         </div>  
  22.         <div class="form-group">  
  23.             <div class="col-md-offset-2 col-md-10">  
  24.                 <input type="submit" value="Submit" class="btn btn-default" />  
  25.                 @ViewBag.Message  
  26.             </div>  
  27.         </div>  
  28.     </div>  
  29. } 

Step 4: Run the application and open the Index view of your controller. Enter some data and you can see the following screenshot while clicking on "Submit".

Model with No Binding

Binding with Simple Types

As you know the DefaultBinder object handles the default Model Binding. The MVC provides the System.Web.Mvc.DefaultModelBinder that is used to work with the simple types or classes and collections. Let's see how it works.

Step 1: Modify the Index() in your controller with the following code:

  1. public ActionResult Index()  
  2. {  
  3.     return View();  
  4. }  
  5. [HttpPost]  
  6. public ActionResult Index(string studentid, string studentfirstname)  
  7. {  
  8.     ViewBag.Message = "Your Student retrieved successfully with an ID" + " " + studentid + "!!";  
  9.     return View();  
  10. } 

Step 2: Now run the controller and enter the id.

Model Binding with Simple Types

Note: Please ensure that the variables you define in the Index() is the same name as you defined in the view. Just view at the following screenshot if we define the following variables:

  1. public ActionResult Index(string studentid, string tudentfirstname) 

Variables in Local

You can see that the null value is assigned in the tudentfirstname variable.

Binding with Class Types

Step 1: Add a class named "Student" in the Models folder and replace the code with the following code:

  1. public class Student  
  2. {  
  3.     public string StudentID { getset; }  
  4.     public string StudentFirstName { getset; }  
  5. } 

Step 2: Now modify the HttpPost Index() method with the following code:

  1. [HttpPost]  
  2. public ActionResult Index(Student s)  
  3. {  
  4.     ViewBag.Message = "Your Student retrieved successfully with an ID" + " " + s.StudentID + "!!";  
  5.     return View();  
  6. } 

Step 3: Now run the application and open the controller.

Model Binding with Class Types

Binding with Class Properties

Step 1: Add a class named "Contact" in the Models folder and replace the code with the following code:

  1. public class Contact  
  2. {  
  3.     public Int64 Mobile { getset; }  
  4.     public string Email { getset; }  
  5. } 

Step 2: Modify the Student class with the following highlighted code:

  1. public class Student  
  2. {  
  3.     public string StudentID { getset; }  
  4.     public string StudentFirstName { getset; }  
  5.     public Contact StudentContact { getset; }  
  6. } 

Step 3: Add the following markup in the Index.cshtml file:

  1. <div class="form-group">  
  2.     @Html.Label("Mobile:"new { @class = "col-md-2 control-label" })  
  3.     <div class="col-md-10">  
  4.         @Html.TextBox("StudentContact.Mobile")  
  5.     </div>  
  6. </div>  
  7. <div class="form-group">  
  8.     @Html.Label("Email:"new { @class = "col-md-2 control-label" })  
  9.     <div class="col-md-10">  
  10.         @Html.TextBox("StudentContact.Email")  
  11.     </div>  
  12. </div> 

Step 4: When you call the StudentContact in the controller and after running the application and providing the information, the defaultbinder maps the property automatically, see in the locals screenshot below:

Local with Class Properties

Summary

This article described the Model Binding with various types like no binding, simple type, class types. In the next part we'll learn more types binding. Thanks for reading.


Similar Articles