Using Interfaces in an action signature of ASP.NET MVC controller

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:53 UTC
Read the original article Hit count: 271

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 other methods and properties*/
  public Bar Data{get;set;}
  string IFoo.Data { get{return Data.ToString()}; set{Data = new Bar(value)}; }      
}
public class MegaController: Controller
{
   public ActionResult Process([ModelBinder(typeof(InterfaceBinder<Foo>))]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