Using Interfaces in an action signature of ASP.NET MVC controller
- by Dmitry Borovsky
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.