我正在学习MVC并在视图中显示产品列表。在视图中使用不同模型的属性(.net MVC)
@model IEnumerable<Domain.Model.Product>
<table>
<tr>
<th style="width:50px; text-align:left">Id</th>
<th style="text-align:left">Name</th>
<th style="text-align:left">Category</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category.Name)
</td>
</tr>
}
</table>
产品属于类别,显示在右栏。我现在想要按类别过滤产品,为此我想使用下拉列表控件。我发现@ Html.DropDownListFor(),但据我所知,这只会给我目前基础模型(产品)的属性。
我的控制器:
public class ProductController : Controller
{
ProductRepository pr = new ProductRepository();
public ActionResult Default()
{
List<Product> products = pr.GetAll();
return View("List", products);
}
}
哎,你需要传递的不仅仅是产品更多。可能是类别和产品的某种连接,或者是新的“模型”类别和产品类别和产品以及一些适合的“查找”方法。 – 2012-01-04 16:20:46