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);
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);
A post RC update to Todd's post:
Adding your model binder provider has been simplified:
An even simple way to add a modelbinder without a ModelBinderProvider is this:
This link, provided by Steve, provides a complete answer. I'm adding it here for reference. Credit goes to dravva on asp.net forums.
First, create a class derived from
IModelBinder
. As Darin says, be sure to use theSystem.Web.Http.ModelBinding
namespace and not the familiar MVC equivalent.Next, provide a provider, which acts as a factory for your new binder, and any other binders you may add in the future.
Finally, include the following in your Global.asax.cs (e.g., Application_Start).
Now, you can just delare the new type as a parameter to your action methods.
or even
There are 2 IModelBinder interfaces:
System.Web.Mvc.IModelBinder
which is the same as in previous versions and hasn't changedSystem.Web.Http.ModelBinding.IModelBinder
which is used by the Web API and the ApiController. So basically inside this method you must set theactionContext.ActionArguments
to the corresponding values. You no longer return a model instance.