August 2011 Blog Posts
Tuesday, August 16, 2011 12:07 PM | Comments

The default Model Binder in ASP.NET MVC works fine for most cases. Most of you have probably registered custom binders with it plenty of times. ModelBinders.Binders.Add(typeof(ILoadProvider), new LoadProviderModelBinder()); The issue is that that it’s limited to binding the exact type you add to its dictionary. The ILoadProvider registered above will invoke my LoadProviderModelBinder as long as the controller action parameter is of type ILoadProvider. But what if you have a type that derives from ILoadProvider and still want your custom binding to occur? Thankfully this is very simple in ASP.NET MVC 3. It comes with a new extensibility point, IModelBinderProvider, and works just like the other providers. public interface IModelBinderProvider { IModelBinder GetBinder(Type modelType); } InheritanceAwareModelBinderProvider With this interface, we are able to create a very simple InheritanceAwareModelBinderProvider. /// <summary> /// Adds inheritance support when registering model binders. /// Any model binders added here will be invoked if the Type being bound inherits...