0

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.

user7090664
  • 45
  • 1
  • 9
  • You passing a single `Product` to a view expecting a collection of `Product` –  Nov 21 '16 at 08:08

2 Answers2

0

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);
abdul
  • 1,562
  • 1
  • 17
  • 22
0

Configuration.ProxyCreationEnabled = false; Add this line to your entity constructor

 public PlatformEntities()
            : base("name=PlatformEntities")
        {
            Configuration.ProxyCreationEnabled = false;
        }

Working With Proxies in Entity Framework

MMM
  • 3,132
  • 3
  • 20
  • 32