I am creating an ASP.NET MVC application using the Entity Framework, but the way I want it
What I'm building now is a page to show all products in a specific category. So if you go, then say, all the products in class / 1 should be shown in Category 1. Currently I am doing this like this. I have this method in the controller: should load all the products in that specific category and send that data into view: This should work, is not it? Not good if I go to Category / 1 then I get the following error: The model item given in the dictionary 'System.Data.Entity.Infrastructure.DbQuery Adding Actually this makes sense, but I do not know any other way to do this. And whatever is, it is that I have followed Microsoft's Asp net tutorial, where they did this. I hope nobody can help me understand it.
public action category (id) {Return View (db.Categories.Where (c => c.CategoryID == ID). Includes = & Gt; C. Products)); }
@EmployableImumerable & webshop.models. Product & gt; @ {ViewBag.Title = "Category:" + @ ViewBag.CategoryName; } & Lt; H2 & gt; @ViewBag.CategoryName & lt; / H2 & gt; & Lt; Table & gt; & Lt; TR & gt; & Lt; Th & gt; @ Html.DisplayNameFor (model => model.firster default (.name) & lt; / Th & gt; & Lt; Th & gt; @ Html.DisplayNameFor (model = & gt; model.firster default (.) Value) & lt; / Th & gt; & Lt; Th & gt; Details & lt; / Th & gt; & Lt; / TR & gt; @Vorch (Various products in the model) {& lt; Tr & gt; & Lt; TD & gt; @ Product.Name & lt; / TD & gt; & Lt; TD & gt; @ Product.Price & lt; / TD & gt; & Lt; Td> @ Html.ActionLink ("Details", "Product", New {ID = Product.ProductIdID}) & lt; / Td> & Lt; / TR & gt; } & Lt; / Table & gt;
1 [ Webshop.models.Category] ', but this model requires a model object called' System.Collections.Generic.IEnumerable '[1] [Webshop.Models.Product]'.
.toList ()
does not work in the controller.
Your view IEnumerable & lt; Webshop.Models.Product & gt;
, but your controller is returning collection of the method category
objects.
I will change my view:
@model Webshop.Models.Category @ {ViewBag.Title = "Category:" + Model.Name; } & Lt; H2 & gt; @ViewBag.CategoryName & lt; / H2 & gt; & Lt; Table & gt; & Lt; TR & gt; & Lt; Th & gt; @ Html.DisplayNameFor (model => model.Products.FirstOrDefault (.name) & lt; / Th & gt; & Lt; Th & gt; @ Html.DisplayNameFor (model = & gt; models.products.firstor Default (). Value) & lt; / Th & gt; & Lt; Th & gt; Details & lt; / Th & gt; & Lt; / TR & gt; @forcha (Product in model. Products) {& lt; Tr & gt; & Lt; TD & gt; @ Product.Name & lt; / TD & gt; & Lt; TD & gt; @ Product.Price & lt; / TD & gt; & Lt; Td> @ Html.ActionLink ("Details", "Product", New {ID = Product.ProductIdID}) & lt; / Td> & Lt; / TR & gt; } & Lt; / Table & gt;
Then your administrator method is:
public action category {int ID} {return see (db.Categories.Where (c = & Gt; c .CategoryID == ID). Finish (C => C. Products). First and default ()); }
You definitely want to make some inquiry to ensure that the category was found rather than returning an empty class because it will be given if given the ID < / Code> was not found.
No comments:
Post a Comment