Custom Model Binding in ASP.NET MVC

custom model binding in mvc

Custom Model Binding

Custom Model Binder can be created by Implementing IModelBinder interface. In this ASP.NET MVC tutorial we are going to see following topics about custom model binding.

  • Creating Example App
  • Create custom model binder by implementing IModelBinder interface
  • Register custom model binder class

Creating Exmaple App :

Create a new web application project in MVC using Empty template and checking the option for adding core references and folders for MVC.

In Models folder I have added a class Addresses.cs having following content :

Add Home controller having following content :

Add view Address.cshtml to support Address action method in HomeController.cs.

In above code I generate list of input boxes to enter addresses if there is no data entered ( when form loads first time ). When user click submit button, address method in HomeController.cs is called to display address details user entered.

If user does’nt enter anything for city or country, there is no appropriate way to disiplay output to user. You can see this when you browse to /Home/Address URL and submit the form without entering any value for city or state.

I can use custom model binder when to display <not specified> to user when there is no value specified for city or country.

Creating Custom Model Binder :

To create custom model binder I have added a new class AddressesBinder implementing interface IModelBinder.

The MVC Framework will call the BindModel method when it wants an instance of the model type that the binder supports.

The parameters to the BindModel method are a ControllerContext object that you can use to get details of the current request and a ModelBindingContext object, which provides details of the model object that is sought, as well as access to the rest of the model binding facilities in the MVC application.

In following table, I have described the most useful properties defined by the ModelBindingContext class.

Property Description
Model Returns model object passed to UpdateModel method if binding has been invoked manually
ModelName Returns name of the model that is being bound
ModelType Returns type of the model that is being created
ValueProvider Returns an IValueProvider implementation that can be used to get data values from the request

When the BindModel method is called, I check to see if the Model property of the ModelBindingContext object has been set. If it has, this is the object that I will generate data value for, and if not, then I create a new instance of the Addresses class.

I get the values for the City and Country properties by calling the GetValue method and return the populated addresses object.

In the GetValue method, I use the IValueProvider implementation obtained from the ModelBindingContext.ValueProvider property to get values for the model object properties.

The ModelName property tells me if there is a prefix I need to append to the property name I am looking for.

You will recall that the action method is trying to create a collection of Addresses objects, which means that the individual input elements will have name attribute values that are prefixed [0] and [1].

As a final step, I supply a default value of <Not Specified> if I can’t find a value for a property or the property is the empty string.

Registering Custom Model Binder :

You have to register custom model binder in global.asax file in Application_start event.

You can test the custom model binder by starting the application and navigating to /Home/Address URL and filling only some of the form elements.
you will see output like shown in figure :

effect using custom model binder

effect using custom model binder 2

Summary :

  • You have to create custom model binder to customize how request data is bound to action method parameters.
  • Custom model binder is created by implementing IModelBinder interface.
  • Custom model binder needs to be registered in global.asax file in Application_Start event.

Leave a Reply

Your email address will not be published. Required fields are marked *