asp.net-mvcinterfacemodelbinders

ASP.NET MVC - Custom Model Binder on Interface Type


I'm not sure if this behavior is expected or not, but it seems that custom model binding doesn't work when the binding is assigned to an interface type. Has anyone experimented with this?

public interface ISomeModel {}
public class SomeModel : ISomeModel {}

public class MvcApplication : HttpApplication {
    protected void Application_Start(object sender, EventArgs e) {
        ModelBinders.Binders[typeof(ISomeModel)] = new MyCustomModelBinder();
    }
}

With the above code when I bind to a model of type SomeModel, MyCustomModelBinder is never hit; however, if I change the above code and substitute typeof(ISomeModel) for typeof(SomeModel) and post the exact same form MyCustomModelBinder is called as expected. Does that seem right?


Edit

I found myself back in this predicament over a year after I originally asked this question, and now I have a solution that works. Thank you Matt Hidinger!

https://web.archive.org/web/20191202065649/http://matthidinger.com:80/archive/2011/08/16/An-inheritance-aware-ModelBinderProvider-in-MVC-3/


Solution

  • I'm not sure if its directly related but yes there are things that you need to think about when using model binding and interfaces... I ran into similar problems with the default model binder, but it may not be directly related depending on how you are doing things...

    Have a look at the following: ASP.net MVC v2 - Debugging Model Binding Issues - BUG? ASP.net MVC v2 - Debugging Model Binding Issues - BUG?