ASP.NET MVC3剃刀查询模型里面(的foreach内的foreach) [英] ASP.NET MVC3 Razor Querying inside a model (foreach within foreach)

查看:1106
本文介绍了ASP.NET MVC3剃刀查询模型里面(的foreach内的foreach)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行到与来自不同实体提取数据视图的问题。基本上我有,汇集类别和产品数据一起桥接实体CategoryProduct。我想最终显示是产品和为每个产品的列表 - 他们的类别。不过我完全卡在如何使最后一部分 - 展示类 - 发生

I'm running into an issue in the View with pulling data from different entities. Basically I have a bridging entity CategoryProduct that brings together Category and Product data together. What I want to ultimately display is a list of products and for each of those products - their categories. However I'm completely stuck on how to make that last part - display categories - happen.

这里的code代表我的模型 -

Here's the code for my model -

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

我的控制器是pretty简单,它只是推动,弥补实体查看:

My Controller is pretty simple, it's just pushing that bridging entity to the View:

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

最后我的视图显示产品和分类,但现在它只是对同一产品创造额外行,如果它有不同的类别,例如产品1:1类产品1:2类等。而且在那里我想要得到的就是产品1:1类,2类

Finally my view displays Products and Categories but right now it's just creating extra rows for the same product if it has a different category, e.g. Product 1: Category 1, Product 1: Category 2 and so on. And Where I want to get to is Product 1: Category 1, Category 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语句无法在类型产品code.Models.CategoryProduct变量操作,因为产品code.Models.CategoryProduct'不包含公共定义为的GetEnumerator

Errors out on: Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'ProductsCode.Models.CategoryProduct' because 'ProductsCode.Models.CategoryProduct' does not contain a public definition for 'GetEnumerator'

编辑:添加其他的foreach循环和误差

added the other foreach loop and the error.

推荐答案

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

Why can't you just change the viewmodel something like this

 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>

这篇关于ASP.NET MVC3剃刀查询模型里面(的foreach内的foreach)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆