Model Binders In ASP.NET MVC

Introduction

In this article we will try to understand what ASP.NET MVC Model Binders are. We will try to implement Model binding to see how they beneficial for all of us.

Background

As we know that when we make a browser request to the class object and the properties name of the class are same as the html control name that data is automatically mapped to the class properties, this mapping is automatic as shown below:

Using the Sample Code

Class Customer with Properties

  1. publicclassCustomer   
  2. {  
  3.   
  4.     publicstring Code  
  5.     {  
  6.         get;  
  7.         set;  
  8.     }  
  9.   
  10.     publicstring Name  
  11.     {  
  12.         get;  
  13.         set;  
  14.     }  
  15. }  
View
  1. <formaction="Customer/Submit" method="post">  
  2.     <table>  
  3.         <tr>  
  4.             <td>CustomerCode</td>  
  5.             <td>  
  6.                 <inputid="Code" name="Code" type="text" />  
  7.             </td>  
  8.         </tr>  
  9.         <tr>  
  10.             <td>Customer Name</td>  
  11.             <td>  
  12.                 <inputid="Name" name="Name" type="text" />  
  13.             </td>  
  14.         </tr>  
  15.         <tr>  
  16.             <td>  
  17.                 <inputid="btnSubmit" name="btnSubmit" type="submit" value="Login" />  
  18.             </td>  
  19.         </tr>  
  20.     </table>  
  21.     </form>  
So now when we submit the form to the controller the form data is automatically mapped to the class object having the same name as html control name as shown below:

User Login
                                 Figure 1: User Login Screen.

Auto model mapping
      Figure 2: Auto model mapping, we can see the form data passed to the class object.

Now what if we have our properties and html control name different let’s try the same and see what happens:

View and Html control name
  1. <div>  
  2.     <formaction="Customer/Submit" method="post">  
  3.         <table>  
  4.             <tr>  
  5.                 <td>CustomerCode</td>  
  6.                 <td>  
  7.                     <inputid="TxtCustomerCode" name="TxtCustomerCode" type="text" />  
  8.                 </td>  
  9.             </tr>  
  10.             <tr>  
  11.                 <td>Customer Name</td>  
  12.                 <td>  
  13.                     <inputid="TxtCustomerName" name="TxtCustomerName" type="text" />  
  14.                 </td>  
  15.             </tr>  
  16.             <tr>  
  17.                 <td>  
  18.                     <inputid="btnSubmit" name="btnSubmit" type="submit" value="Login" />  
  19.                 </td>  
  20.             </tr>  
  21.         </table>  
  22.         </form>  
Class Properties
  1. publicclassCustomer   
  2. {  
  3.   
  4.     publicstring Code  
  5.     {  
  6.         get;  
  7.         set;  
  8.     }  
  9.   
  10.     publicstring Name   
  11.     {  
  12.         get;  
  13.         set;  
  14.     }  
  15. }  
UI Screen
                                             Figure 3: UI Screen

Hence we can see below as soon as the name is different the form data is not automatically mapped.

map with class object
                                 Figure 4: Form data doesn’t map with class object

As we know that User Interface are designed by the Web Developers and C# created by Developers. So they can change the name to their preference, then the auto mapping won’t happen as we have seen above because our property name and user interface name is different the form data is not bind to the class object. So here Model Binder comes into picture. Model Binder contains a code which maps the browser request to data object.

The System.Web.MvcNamespace provides the concrete implementation of a model binder.

In order to implement Model binder we have to implement IModelBinder interface as shown below.

publicclassCustomerBinder : IModelBinder

For this we create our class called CustomerBinder and implement IModelBinder interface and then we implement the Interface method.

IModelBinder Interface
                     Figure 5: Demonstrating Implementation of IModelBinder Interface

As we know the Web works on simple principle of Request and Response. The User sends the request to the server with the help of browser and server interprets the request and gives desired response to the User on the browser.

Web working
                        Figure 6: Simple Demonstration of Web working

Now both of the Request and Response object belongs to Context object. Let’s get access to Request and Response object and map them together as shown below:
  1. publicclassCustomerBinder: IModelBinder   
  2. {  
  3.   
  4.     publicobjectBindModel(ControllerContextcontrollerContext, ModelBindingContextbindingContext)  
  5.     {  
  6.         HttpContextBaseobjContext = controllerContext.HttpContext;  
  7.         stringCustomerCode = objContext.Request.Form["TxtCustomerCode"]; //binding with UI txtCustomerCode  
  8.         stringCustomerName = objContext.Request.Form["TxtCustomerName"]; //binding with UI txtCustomerName  
  9.         CustomerobjCustomer = newCustomer() {  
  10.             Code = CustomerCode, //mapping to the properties  
  11.                 Name = CustomerName //mapping to the properties  
  12.         };  
  13.         returnobjCustomer; //returning the object  
  14.   
  15.     }  
  16. }  
Now we will use this class and map to the CustomerObject, in the Submit Action as shown below:
  1. publicActionResult Submit([ModelBinder(typeof(CustomerBinder))] Customerobj)  
  2.     //call the CustomerBinder class and fill this Customer Object   
  3.     {  
  4.         if (ModelState.IsValid) {  
  5.             return View("Customer", obj);  
  6.         } else {  
  7.             return View("Index", obj);  
  8.         }  
  9.     }  
([ModelBinder(typeof(CustomerBinder))] Customerobj
Now the above Class CustomerBinder will be called and from the Http Context we can get the Request.Form data and bind it to the Class properties.

Model Binding
                                                            Figure 7:Model Binding 

Model Binding

Now we can see that the mapping has been done by Model Binder class, now developers and designers can work separately. So we can see how Model Binders help us to map the UI data with Class properties. It would be easy that we are not dependent upon the UI controls Name we can simply put the Name in the Model Binder mapping code.

Conclusion

In this article we specially focused about Model Binders and how we can bind different name UI controls with different properties. Your comments, votes and suggestions motivate us for writing more stuff like this.


Similar Articles