Click here to Skip to main content
15,897,891 members
Articles / Programming Languages / C#
Tip/Trick

Model Binder Basics and Custom Model Binder

Rate me:
Please Sign up or sign in to vote.
4.70/5 (7 votes)
25 Aug 2015CPOL4 min read 15.6K   8   2
Model Binder Basics and Custom Model Binder

Introduction

Usually when any request is made from client side to server side, we have to pass some data from client side to server side.Using this data server side logic will do some manipulations to achieve the desired output which is displayed to UI. Usually in web forms, there are many ways in which this can be achieved like using ajax calls OR if I would like to pass entire fields from client browser to server methods then I would say FormCollection object. For developers which are coming from Web forms and started development with MVC, there is a huge curiosity to know the magic of how the form fields are getting assigned to Model properties.

Here comes the Model Binder in picture.

Background

Just to explain the background or I would like to say the time line for Model binder, I have created a MVC application. Consider I have created a Model with Name PersonalInfo as below.

Image 1

Image 2

I simply created a view which will take three inputs from user(First Name,Last Name and Age).

Image 3

User will fill the data and will click on Save. On click of Save an action method will be called. In this action method, I have used a FormCollection class object. The object will contain all the fields from page. If I want to assign the values from Page to PersonalInfo class object then I need to assign these values from FormCollection.

Image 4

Image 5

In this way if you want to post data from page to action method ,then you have to manually map all the fields from Page to respective properties

Also you need to perform the type casting of properties as and when required.

So there will a problem to map all the input values when we are having a lot of properties of different data types in our Model as well as when in a model we are having some properties which belong to another model

To overcome this issue and to make or life little bit easier we can use the power of one of the feature provided by MVC Framework which is none other than "Model Binding"

What is Model Binding?

So from above discussion we can say that MODEL BINDING is a feature of ASP.NET MVC which allows developers to pass or I would say map all the data in input fields from client side to corresponding server side objects of respective action method without any manual work of assigning the input fields to respective properties and without type casting.

Using the code

We will see how this can be dne with our previous example.Now I will not use the FormCollection object .Instead of FormCollection object, I will use the object of PersonalInfo class. You will be able to see the magic of model binder. All the fields will get automatically assigned to respective properties of that class.

Image 6

Here Please make a note that the Name attribute of the input field from HTML should match the name of the property from the Model because internally Model Binding will be performed on the Name attribute of the field. For example, name attribute of the First Name is FName and in PersonalInfo class we have it as FirstName then at save action method the value of First name will not get mapped to FirstName property of PersonalInfo class object.

Image 7

However there are some scenario's where We need to customize the default model binding to work differently.

I will explain you one simple scenario with our previous example.

I want to have Full Name as one property in my Model, but on page we are having two fields one for First Name and one for Last Name. As our model class will not contain FirstName and LastName properties, it will contain only FullName property. In this situation default model binding will not work.

Here comes the need to have Custom Model Binding

Custom Model Binding

So now We will go through how model binder will work if we have view fields that are incompatible with Model. To customize the default model binding mechanism, we need to make use of IModelBinder interface OR we need to derive our class from DefaultModelBinder class.

DefaultModelBinder class implements IModelBinder interface.

Let me change our previous Model class PersonalInfo. Here I have added some validation attributes but I will not go into details as these are out of scope for this article.

Image 8

I have added one class with name PersonalInfoModelBinder.cs. This class will implement IModelBinder interface to combile first Name and Last Name and PersonalInfo object will be returned from the method.

Image 9

Image 10

For Save action method we need to include the references for custom model binder

Image 11

Now we have to register this custom model binder in global.asax

Image 12

Here we go, we are done with the custom model binder. The combined First Name and Last Name will be displayed in FullName property.

Image 13

Points of Interest

Comments and suggestions are most welcome.

License

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


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNice Pin
BartekKw26-Aug-15 2:21
BartekKw26-Aug-15 2:21 
GeneralGreate Pin
Aladár Horváth25-Aug-15 20:39
professionalAladár Horváth25-Aug-15 20:39 
Thumbs Up | :thumbsup:

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

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