【问题标题】:ASP.NET MVC3 Razor Querying inside a model (foreach within foreach)模型内的 ASP.NET MVC3 Razor 查询(foreach 内的 foreach)
【发布时间】:2012-05-01 01:44:03
【问题描述】:

我在视图中遇到了从不同实体提取数据的问题。基本上,我有一个桥接实体 CategoryProduct ,它将类别和产品数据结合在一起。我最终想要展示的是一个产品列表以及每个产品的类别——它们的类别。但是,我完全不知道如何使最后一部分 - 显示类别 - 发生。

这是我的模型的代码 -

public class Product
    {   
        public int ProductId { get; set; }
        public string Title { get; set; }
        public virtual ICollection<CategoryProduct> CategoryProducts { get; set; }
    }

    public class CategoryProduct
    {
        public int CategoryProductID { get; set; }
        public int CategoryId { get; set; }
        public int ProductId { get; set; }
        public virtual Product Product { get; set; }
        public virtual Category Category { get; set; }
    }

    public class Category
    {
        public int CategoryId { get; set; }
        public string Title { get; set; }
        public virtual ICollection<CategoryProduct> CategoryProducts { get; set; }
    }

我的控制器非常简单,它只是将桥接实体推送到视图:

public ActionResult Index()
{
    return View(db.CategoryProducts.ToList());
}

最后我的视图显示产品和类别,但现在它只是为同一产品创建额外的行,如果它有不同的类别,例如产品 1:类别 1,产品 1:类别 2,依此类推。我想去的是产品 1:类别 1,类别 2。

@model IEnumerable<ProductsCode.Models.CategoryProduct>
<h2>Index</h2>
<table>
    <tr>
        <th>Title</th>
        <th>Category</th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Product.Title)
        </td>
        <td>
           @foreach (var categories in item)
           {
             @Html.DisplayFor(modelItem => item.Why.Title)            
           }   
        </td>
    </tr>
}
</table>

错误:编译器错误消息:CS1579:foreach 语句无法对“ProductsCode.Models.CategoryProduct”类型的变量进行操作,因为“ProductsCode.Models.CategoryProduct”不包含“GetEnumerator”的公共定义

编辑:添加了另一个 foreach 循环和错误。

【问题讨论】:

  • 我没有看到 foreach 包含在 foreach 中。
  • @KirkWoll 是的,这就是我在 foreach 中遇到的问题 - 我收到一个错误,说它不是 IEnumerable
  • 它在哪里抱怨它不是IEnumerable? 什么是不是IEnumerable?
  • 按设计为每个产品/类别组合显示一行。您的 CategoryProduct 模型是每个产品和类别的组合。尝试使用您的产品模型,然后从 CategoryProducts 中提取一个 IEnumberable 类别。
  • 我会说重新看看你的模型设计。通常,Product 类应该有一个对 Category 的引用,Category 类应该有一个对 Products 集合的引用。

标签: c# asp.net-mvc razor foreach


【解决方案1】:

为什么你不能像这样改变视图模型

 public class MyProduct
    {   
        public int ProductId { get; set; }
        public string Title { get; set; }
        public virtual ICollection<Category> CategoryList { get; set; }
    }

并查看

@model IEnumerable<ProductsCode.Models.MyProduct>
<h2>Index</h2>
<table>
    <tr>
        <th>Product</th>
        @Html.DisplayFor(modelItem => item.ProductId) 
        <th>Categories for Product</th>
    </tr>

@foreach (var item in Model.CategoryList) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CategoryId)
        </td>
    </tr>
}
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多