在MVC视图中的Foreach

Gui*_*tin 7 c# asp.net asp.net-mvc entity-framework asp.net-mvc-4

大编辑:我已经编辑了我的完整帖子,答案是我在Von V和Johannes的帮助下提出的,非常感谢你!

我一直在尝试在索引视图中的foreach循环中执行foreach循环,以便在手风琴中显示我的产品.让我告诉你我是如何做到这一点的.

这是我的模特:

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; }
}
Run Code Online (Sandbox Code Playgroud)

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

以下是我在我看来要做的事情:

@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>
Run Code Online (Sandbox Code Playgroud)

我不太确定这是正确的做法,但这正是我想做的事情.Foreach类别,将该类别的所有产品放入手风琴选项卡中.

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

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

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>();
    }
}
Run Code Online (Sandbox Code Playgroud)

我还将添加我的控制器,以便您可以看到我是如何做到的:

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

大编辑:我已经编辑了我的完整帖子,答案是我在Von V和Johannes的帮助下提出的,非常感谢你!

Joh*_*udi 10

假设你的控制器的动作方法是这样的:

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

修改您的模型是这样的:

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; }
}
Run Code Online (Sandbox Code Playgroud)

然后你从现在起控制器将类别作为模型(而不是产品):

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>
}
Run Code Online (Sandbox Code Playgroud)

更新:将ToList()添加到控制器返回语句.