ASP.NET MVC Model Binders
One thing that I’m always asked to talk about when I’m delivering ASP.NET MVC sessions or courses is Model Binders. In this post I’ll explain what are Model Binders in ASP.NET MVC and how you can use them in your MVC application.
ASP.NET MVC is providing a way to de-serialize complex types that are part of the incoming HTTP request input. After these types are de-serialize they are passed to the relevant controller’s action as method arguments. In the following controller action method code, during the execution of ASP.NET MVC pipeline, a binder is used to create the Customer instance from the request input:
ASP.NET MVC supplies some binders out of the box such as DefaultModelBinder that is using conventions (such as naming conventions) in order to create the model from the request or FormCollectionModelBinder that maps a request to a FormCollection object. You can use those defaults and of course you can create your own Model Binders.
Creating a Custom Model Binder
In order to create a Model Binder you need to get familiar with the IModelBinder interface:
This interface declare the BindModel method which is the only method that you need to implement. The method gets as arguments the controller context and the model binding context. The ControllerContext object holds information about the current HTTP request and about the associated controller. The ModelBindingContext holds information about the model itself and its metadata. In most of the time you won’t have to write your own binder but if you need to then follow the example.
Custom Model Binder Example
In the example I’ll use the following Course presentation model object:
Here is a simple implementation of a Model Binder for the Course model:
The binder will create the instance of the course by converting all the relevant types from the incoming HTTP request.
Remark: The DefaultModelBinder will also do the same thing since it will use the naming conventions and the property types in order to make the conversion. This is only a simple example in order to explain the concept of Model Binders.
Integrating a Custom Model Binder
In order to integrate a Model Binder into your application you’ll use the ModelBinders class and its Binders collection. Here is how you’ll integrate the previous binder:
The perfect place to put this line of code is in the Application_Start event in the Global.asax file.
Model Binders are very useful and saves us a lot of development time. They help us to transform HTTP request input into model instances automatically and by that making our life easier. When in need, you can create your own custom Model Binder and integrate it into your MVC application.