Search code examples
asp.net-mvcasp.net-mvc-4asp.net-web-apiimodelbinder

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


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);

Solution

  • 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.