views:

214

answers:

1

I am getting the following Error The model item passed into the dictionary is of type 'MvcWebApplication.Models.Product' but this dictionary requires a model item of type 'MvcWebApplication.ViewModels.ProductCommonViewModel'.

Controller Code is as follows:-

public ActionResult Index([Bind(Prefix= "MvcWebApplication.ViewModels.ProductCommonViewModel")] Product product)
View Model class sent to the View:
namespace MvcWebApplication.ViewModels
{
    public class ProductCommonViewModel
    {
        public Product Product { get; set; }
    }
}

Model Binder :

namespace MvcWebApplication.ModelBinders
{
    public class ProductBinder : IModelBinder
    {
        #region IModelBinder Members

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase fc = controllerContext.HttpContext.Request;

            Product product = new Product();
            LaunchSheet launchSheet = null; ;
            ProductPricing productPricing = null;

            product.LoanTypeId = Convert.ToInt32(fc["rblLoanType"]);
            product.ProductTypeId = Convert.ToInt32(fc["ddlproductType"]);
etc...
            return Product;
          }
      }
}

Product Class

[ModelBinder(typeof(ProductBinder))]
    public partial class Product : IRuleEntity
    {

}

What am I doing wrong?

+2  A: 

It's not the binding which is broken. The problem is that your strongly-typed view (aspx/ascx) requires a model of type MvcWebApplication.ViewModels.ProductCommonViewModel, but the action which renders it is passing a model of type MvcWebApplication.Models.Product. Note that this has nothing to do with the code in your question; the bug is in the "return View(model)" line elsewhere.

Craig Stuntz