Asp.net mvc 2 NETMVC2。无法创建接口的实例
我的项目使用ASP.NETMVC2 我正在尝试使用下一个代码编辑我的产品信息:Asp.net mvc 2 NETMVC2。无法创建接口的实例,asp.net-mvc-2,interface,Asp.net Mvc 2,Interface,我的项目使用ASP.NETMVC2 我正在尝试使用下一个代码编辑我的产品信息: [HttpGet] public ActionResult Edit(int id) { IProduct product = productService.getProductById(id); return View(product); } [HttpPost] public ActionResult Edit(IProduct p
[HttpGet]
public ActionResult Edit(int id)
{
IProduct product = productService.getProductById(id);
return View(product);
}
[HttpPost]
public ActionResult Edit(IProduct product)
{
//Whatever i did here i always get the error prevents my any actions
}
IPProduct和其他实体正在使用IoC Castle Windsor实例化。用于编辑的页面已成功加载。在我的页面顶部,我声明页面必须继承=“System.Web.Mvc.ViewPage”〈DomainModel.Entities.IPProduct〉. 确实如此。但当我试图使用下一个代码更新我的更改时:
[HttpGet]
public ActionResult Edit(int id)
{
IProduct product = productService.getProductById(id);
return View(product);
}
[HttpPost]
public ActionResult Edit(IProduct product)
{
//Whatever i did here i always get the error prevents my any actions
}
然后我得到错误“无法创建接口实例”
有人能澄清原因吗?
很抱歉我的英语可能不好,谢谢你的回答。这是因为MVC需要知道一个具体的类型来为你的产品参数构造 不能实例化IPProduct,只能实例化实现IPProduct的具体类型。最简单的方法是使用简单的具体DTO类型从视图(有时称为ViewModel)和视图(有时称为ViewModel)传输数据
如果为接口类型实现了自定义模型绑定器,则可以使示例与接口类型一起工作。(基本上是一种告诉MVC在遇到IPProduct时要实例化什么类型的方法)。对于这种情况,您有自定义模型绑定器的示例吗?