我不知道你的理由是什麼。我假設他們是有效的。 MVC不會爲你的界面提供實現。你將不得不覆蓋默認的模型綁定的行爲像下面提供的具體類型(它可以來自你的IOC容器):
public class MyBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext
, ModelBindingContext bindingContext, Type modelType)
{
if (bindingContext.ModelType.Name == "ISomeInterface")
return new SomeType();
//You can get the concrete implementation from your IOC container
return base.CreateModel(controllerContext, bindingContext, modelType);
}
}
public interface ISomeInterface
{
string Val { get; set; }
}
public class SomeType : ISomeInterface
{
public string Val { get; set; }
}
然後在你的應用程序開始看起來像下面:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new MyBinder();
//Followed by other stuff
}
}
這裏的工作行爲
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
var routeValueDictionary = new RouteValueDictionary()
{
{"id",1},
{"Val","test"}
};
return RedirectToAction("Abouts", "Home", routeValueDictionary);
}
public ActionResult Abouts(int id, ISomeInterface testInterface)
{
ViewBag.Message = "Your application description page.";
return View();
}
That's不可能的,除非你使用依賴注入提供商,告訴什麼是執行或您創建該類型和國防部一個模型綁定器資料夾決定什麼是實施。 – 2015-03-31 17:30:06