[英]Transferring data from controller to view - List<> / IEnumerable<>?
I'm building an asp.net MVC application using the Entity Framework, but it's not working the way I want it to. 我正在使用实体框架构建asp.net MVC应用程序,但是它无法按照我想要的方式工作。
This is my model (not all model classes, but that's not relevant): 这是我的模型(并非所有模型类,但这都不相关):
public class Product
{
public Product()
{
Categories = new List<Category>();
}
public int ProductID { get; set; }
public byte[] Image { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public Offer Offer { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
public Category()
{
Products = new List<Product>();
}
public int CategoryID { get; set; }
public byte[] Image { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
What I am building now is a page to show all products in a specific category. 我现在正在构建的页面显示了特定类别中的所有产品。 So if you go to, let's say, /Category/1 it should show all products in category 1. Currently I am doing it this way. 因此,如果您转到/ Category / 1,它应该显示类别1中的所有产品。目前,我正在这样做。 In the controller I have this method: 在控制器中,我有这种方法:
public ActionResult Category(int ID)
{
return View(db.Categories.Where(c => c.CategoryID == ID).Include(c => c.Products));
}
which should load all products of that specific category and send that data to the view: 它将加载该特定类别的所有产品,并将该数据发送到视图:
@model IEnumerable<Webshop.Models.Product>
@{
ViewBag.Title = "Category: " + @ViewBag.CategoryName;
}
<h2>@ViewBag.CategoryName</h2>
<table>
<tr>
<th>@Html.DisplayNameFor(model => model.FirstOrDefault().Name)</th>
<th>@Html.DisplayNameFor(model => model.FirstOrDefault().Price)</th>
<th>Details</th>
</tr>
@foreach (var product in Model)
{
<tr>
<td>@product.Name</td>
<td>@product.Price</td>
<td>@Html.ActionLink("Details", "Product", new { ID = product.ProductID })</td>
</tr>
}
</table>
This should work, right? 这应该工作,对不对? Well it's not. 好吧,不是。 If I go to /Category/1 I get the following error: 如果我转到/ Category / 1,则会出现以下错误:
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery
1[Webshop.Models.Category]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[Webshop.Models.Product]'. 传递到词典中的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery1[Webshop.Models.Category]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [Webshop1[Webshop.Models.Category]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
.Models.Product]”。
Adding .ToList()
in the controller doesn't work. 在控制器中添加.ToList()
不起作用。
Actually it makes sense, but I don't know any other way to do this. 实际上这是有道理的,但是我不知道有其他方法可以做到这一点。 And what is even weirder is that I followed an asp.net tutorial from Microsoft where they did exactly this. 而且甚至更奇怪的是,我遵循了Microsoft的asp.net教程,他们正是这样做的。
I hope anyone can help me figure this out. 我希望任何人都可以帮助我解决这个问题。
Your view is expecting a model of IEnumerable<Webshop.Models.Product>
, but your controller method is returning a collection of Category
objects. 您的视图期望使用IEnumerable<Webshop.Models.Product>
模型,但是您的控制器方法将返回Category
对象的集合。
I'd change your view to this: 我会改变您的看法:
@model Webshop.Models.Category
@{
ViewBag.Title = "Category: " + Model.Name;
}
<h2>@ViewBag.CategoryName</h2>
<table>
<tr>
<th>@Html.DisplayNameFor(model => model.Products.FirstOrDefault().Name)</th>
<th>@Html.DisplayNameFor(model => model.Products.FirstOrDefault().Price)</th>
<th>Details</th>
</tr>
@foreach (var product in Model.Products)
{
<tr>
<td>@product.Name</td>
<td>@product.Price</td>
<td>@Html.ActionLink("Details", "Product", new { ID = product.ProductID })</td>
</tr>
}
</table>
Then your controller method becomes this: 然后,您的控制器方法将变为:
public ActionResult Category(int ID)
{
return View(db.Categories.Where(c => c.CategoryID == ID).Include(c => c.Products).FirstOrDefault());
}
You'd probably want to do some check to make sure the category was found instead of returning a null Category as this will do if the given ID
isn't found. 您可能需要做一些检查以确保找到了类别,而不是返回空的类别,因为如果找不到给定的ID
它将这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.