How do I create a custom model binder using the `BindModel(HttpActionContext actionContext...` signature?

Mohammad HS Farvashani picture Mohammad HS Farvashani · Mar 28, 2012 · Viewed 14.7k times · Source

I need to know how to create a custom IModelBinder in MVC 4 and it has been changed.

The new method that has to be implemented is :

bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext);

Answer

Darin Dimitrov picture Darin Dimitrov · Mar 28, 2012

There are 2 IModelBinder interfaces:

  1. System.Web.Mvc.IModelBinder which is the same as in previous versions and hasn't changed
  2. System.Web.Http.ModelBinding.IModelBinder which is used by the Web API and the ApiController. So basically inside this method you must set the actionContext.ActionArguments to the corresponding values. You no longer return a model instance.