Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Model Binder Basics and Custom Model Binder

0.00/5 (No votes)
25 Aug 2015 1  
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.

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

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.

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.

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.

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.

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.

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

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

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

Points of Interest

Comments and suggestions are most welcome.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here