user7090664 user7090664 - 14 days ago 6
ASP.NET (C#) Question

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies

I am new to asp.net MVC. I have two tables named as Category and Product. Product table has foreign key which CategoryID. I am able to display category on a page. Lets take one example. I have two categories named Mobiles and Computers. When I click in Computer, I would like to display products related to Computer Category.

I have attached codes. Kindly, help me with this.

public ActionResult ProductList(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
}


Here is model:
@model IEnumerable<ReSale.Models.Product>

I am using IEnumerable. I am getting above error.

Answer

You are trying to return a single result with IEnumerable model, that's the reason you are getting this error. Try this approach:

var products = db.Products.Where(pr => pr.Id == id)
                      .Take(1)
                      .ToList();

return View(products);
Comments