Using Interfaces in action signature

Posted by Dmitry Borovsky on Stack Overflow See other posts from Stack Overflow or by Dmitry Borovsky
Published on 2010-04-07T10:39:33Z Indexed on 2010/04/07 10:43 UTC
Read the original article Hit count: 332

Hello, I want to use interface in Action signature. So I've tried make own ModelBinder by deriving DefaultModelBinder:

public class InterfaceBinder<T> : DefaultModelBinder
        where T: new()
{
  protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
  {
     return base.CreateModel(controllerContext, bindingContext, typeof(T));
  }
}

public interface IFoo
{
  string Data { get; set; }
}
public class Foo: IFoo /*other interfaces*/
{
  /* a lot of */
  public string Data { get; set; }      
}
public class MegaController: Controller
{
   public ActionResult Process(

[ModelBinder(typeof(InterfaceBinder))]IFoo foo){/bla-bla-bla/} }

But it doesn't work. Does anybody have idea how release this behaviour? And yes, I know that I can make my own implementation of IModelBinder, but I'm looking for easier way.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about interface