views:

68

answers:

1

I use ASP.NET MVC 2 for my project.

I'm trying to edit my product information using next code:

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

IProduct and other IEntities are instansing using IoC Castle Windsor. Page for Edit is succesfully loaded. In the top of my page I declared that page must Inherits="System.Web.Mvc.ViewPage〈DomainModel.Entities.IProduct〉. And it is. But when I'm trying to update my changes using next code:

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

then i'm getting the error 'Cannot create an instance of an interface.'

Can anybody clarify why? Sorry for probably bad English, and thanks for any answer.

+1  A: 

This is because MVC needs to know a concrete type to construct for your product parameter.

You cannot instantiate an IProduct, only a concrete type that implements IProduct. The easiest way to make this work, is to use simple concrete DTO types to transfer data from and to the view (sometimes known as the ViewModel).

You can make your example work with the interface type, if you implement a custom model binder for the type. (Basically a way to tell MVC what type to instantiate when it encounters IProduct).

driis