Click here to Skip to main content
Click here to Skip to main content
Technical Blog

What is the need of ASP.NET MVC model binders?

, 14 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Model binder maps HTML form elements to the model. It acts like a bridge between HTML UI and MVC model.Take the below simple HTML form example :-Now this form needs to fill the below “Customer” class model. If you see the HTML control name they are different from the class property name. For example
Model binder maps HTML form elements to the model. It acts like a bridge between HTML UI and MVC model.
 
 
Take the below simple HTML form example :-
 
<formid="frm1" method=post action=&rdquo;/Customer/SubmitCustomer&rdquo;>
            Customer code :- <inputname="CCode"type="text"/>
            Customer name :- <inputname="CName"type="text"/>
	<input type=submit/>
</form>
Now this form needs to fill the below “Customer” class model. If you see the HTML control name they are different from the class property name. For example HTML textbox control name is “CCode” and the class property name is “CustomerCode”.  This mapping code is written in HTML binder classes.
 
publicclassCustomer
{
publicstring CustomerCode { get; set; }
publicstring CustomerName { get; set; }
}
 
To create a model binder we need to implement “IModelBinder” interface and mapping code needs to be written in the “BindModel” method as shown in the below code.
 
publicclassCustomerBinder : IModelBinder
{

publicobject BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
HttpRequestBase request = controllerContext.HttpContext.Request;

string strCustomerCode = request.Form.Get("CCode");
string strCustomerName = request.Form.Get("CName");

returnnewCustomer
            {
                CustomerCode = strCustomerCode,
                CustomerName = strCustomerName
            };
}
}
 
Now in the action result method we need to use the “ModelBinder” attribute which will attach the binder with the class model.
 
publicActionResult SubmitCustomer([ModelBinder(typeof(CustomerBinder))]Customer obj)        {

return View(&ldquo;DisplayCustomer&rdquo;);
}
 
In case you are new to MVC you can start from the below www.questpond.com video 
 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Comments and Discussions

 
QuestionMy votes for 5 PinmemberGaurav.Kumar.Arora27-Oct-14 10:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 14 Jun 2014
Article Copyright 2014 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid