在MVC视图中的Foreach中的Foreach [英] Foreach in a Foreach in MVC View

查看:98
本文介绍了在MVC视图中的Foreach中的Foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大我编辑了我的完整帖子,答案是在冯·V和约翰内斯的帮助下提出的,我非常感谢你们!!!!

BIG EDIT : I have edited my full post with the answer that I came up with the help of Von V and Johannes, A BIG THANK YOU GUYS !!!!

我一直在尝试在索引视图的foreach循环内执行一个foreach循环,以在手风琴中显示我的产品.让我向您展示我如何尝试做到这一点.

I've been trying to do a foreach loop inside a foreach loop in my index view to display my products in an accordion. Let me show you how I'm trying to do this.

这是我的模特:

public class Product
{
    [Key]
    public int ID { get; set; }

    public int CategoryID { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public string Path { get; set; }

    public virtual Category Category { get; set; }
}

public class Category
{
    [Key]
    public int CategoryID { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

这是一个一对一一对多的关系,一个产品只有一个类别,但是一个类别中有很多产品.

It's a one-one one-many relationship, One product has only one category but a category had many products.

这是我想做的事情:

@model IEnumerable<MyPersonalProject.Models.Product>   

    <div id="accordion1" style="text-align:justify">
    @foreach (var category in ViewBag.Categories)
    {
        <h3><u>@category.Name</u></h3>

        <div>

            @foreach (var product in Model)
            {
                if (product.CategoryID == category.CategoryID)
                {
                    <table cellpadding="5" cellspacing"5" style="border:1px solid black; width:100%;background-color:White;">
                        <thead>
                            <tr>
                                <th style="background-color:black; color:white;">
                                    @product.Title  
                                    @if (System.Web.Security.UrlAuthorizationModule.CheckUrlAccessForPrincipal("/admin", User, "GET"))
                                    {
                                        @Html.Raw(" - ")  
                                        @Html.ActionLink("Edit", "Edit", new { id = product.ID }, new { style = "background-color:black; color:white !important;" })
                                    }
                                </th>
                            </tr>
                        </thead>

                        <tbody>
                            <tr>
                                <td style="background-color:White;">
                                    @product.Description
                                </td>
                            </tr>
                        </tbody>      
                    </table>                       
                }
            }

        </div>
    }  
</div>

我不太确定这是正确的方法,但这几乎就是我要尝试的方法.对于每个类别,请将该类别的所有产品放入手风琴标签中.

I'm not quite sure this is the right way of doing it but this is pretty much what I'm trying to do. Foreach categories, put all products of that categories inside an accordion tab.

  • 类别1
    • 产品1
    • 产品3
    • 产品2
    • 产品4
    • 产品5

    在这里,我将为我的一对一一对多(感谢Brian P)关系添加映射:

    Here I will add my mapping for my one-one one-many (Thanks Brian P) relationship :

    public class MyPersonalProjectContext : DbContext
    {
        public DbSet<Product> Product { get; set; }
        public DbSet<Category> Category { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
            modelBuilder.Entity<Product>();
            modelBuilder.Entity<Category>();
        }
    }
    

    我还将添加我的控制器,以便您了解我的操作方式:

    I will also add my controller so you can see how I did it :

    public ActionResult Index()
        {
            ViewBag.Categories = db.Category.OrderBy(c => c.Name).ToList();
            return View(db.Product.Include(c => c.Category).ToList());
        }
    

    大我编辑了我的完整帖子,答案是在冯·V和约翰内斯的帮助下提出的,我非常感谢你们!!!!

    BIG EDIT : I have edited my full post with the answer that I came up with the help of Von V and Johannes, A BIG THANK YOU GUYS !!!!

    推荐答案

    假设您控制器的操作方法如下:

    Assuming your controller's action method is something like this:

    public ActionResult AllCategories(int id = 0)
    {
        return View(db.Categories.Include(p => p.Products).ToList());
    }
    

    将模型修改为如下形式:

    Modify your models to be something like this:

    public class Product
    {
        [Key]
        public int ID { get; set; }
        public int CategoryID { get; set; }
        //new code
        public virtual Category Category { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string Path { get; set; }
    
        //remove code below
        //public virtual ICollection<Category> Categories { get; set; }
    }
    
    public class Category
    {
        [Key]
        public int CategoryID { get; set; }
        public string Name { get; set; }
        //new code
        public virtual ICollection<Product> Products{ get; set; }
    }
    

    然后您从现在开始,控制器将类别作为模型(而不是产品):

    Then your since now the controller takes in a Category as Model (instead of a Product):

    foreach (var category in Model)
    {
        <h3><u>@category.Name</u></h3>
        <div>
            <ul>    
                @foreach (var product in Model.Products)
                {
                    // cut for brevity, need to add back more code from original
                    <li>@product.Title</li>
                }
            </ul>
        </div>
    }
    

    已更新:将ToList()添加到控制器的return语句中.

    UPDATED: Add ToList() to the controller return statement.

    这篇关于在MVC视图中的Foreach中的Foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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