ASP.NET MVC 2.无法创建接口的实例

int*_*t32 2 interface asp.net-mvc-2

我将ASP.NET MVC 2用于我的项目.

我正在尝试使用下一代码编辑我的产品信息:

    [HttpGet]
    public ActionResult Edit(int id)
    {
        IProduct product = productService.getProductById(id);
        return View(product);
    }
Run Code Online (Sandbox Code Playgroud)

IP产品和其他IEntities使用IoC Castle Windsor进行实例化.已成功加载编辑页面.在我的页面顶部,我声明该页面必须是Inherits ="System.Web.Mvc.ViewPage <DomainModel.Entities.IProduct>.并且确实如此.但是当我尝试使用下一个代码更新我的更改时:

    [HttpPost]
    public ActionResult Edit(IProduct product)
    {
        //Whatever i did here i always get the error prevents my any actions
    }
Run Code Online (Sandbox Code Playgroud)

然后我收到错误'无法创建接口的实例'.

谁能澄清为什么?对不起,可能是英语不好,谢谢你的回答.

dri*_*iis 5

这是因为MVC需要知道要为您的product参数构造的具体类型.

您无法实例化IProduct,只能实现IProduct的具体类型.使这项工作最简单的方法是使用简单的具体DTO类型从视图传输数据(有时称为ViewModel).

如果为该类型实现自定义模型绑定器,则可以使示例使用接口类型.(基本上是一种告诉MVC遇到IProduct时要实例化的类型的方法).