Saturday 15 January 2011

c# - Transferring data from controller to view - List<> / IEnumerable<>? -


I am creating an ASP.NET MVC application using the Entity Framework, but the way I want it

public class product {public product} (categories = New list & lt; grade & gt;); } Public int ProductID {get; Set; } Public byte [] image {get; Set; } Public string name {get; Set; } Public string description {get; Set; } Public decimal value {received; Set; } Receive public proposal proposal; Set; } Public virtual iconging & lt; Category & gt; Categories {Get; Set; }} Public category category {public category () {product = new list & gt; Products & gt; (); } Public Integer ClassIID {Received; Set; } Public byte [] image {get; Set; } Public string name {get; Set; } Public string description {get; Set; } Public Virtual Ilectronics & lt; Products & gt; Product {Received; Set; }}

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:

  public action category (id) {Return View (db.Categories.Where (c => c.CategoryID == ID). Includes = & Gt; C. Products)); }  

should load all the products in that specific category and send that data into view:

  @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;  

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 1 [ Webshop.models.Category] ', but this model requires a model object called' System.Collections.Generic.IEnumerable '[1] [Webshop.Models.Product]'.

Adding .toList () does not work in the controller.

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.

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