title: Foreach in a Foreach in MVC View
body: <p>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 !!!!</p>
  
  <p>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.</p>
  
  <p>Here are my models :</p>
  
  <pre class="lang-cs prettyprint-override"><code>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&lt;Product&gt; Products { get; set; }
  }
  </code></pre>
  
  <p>It's a <strike>one-one</strike> one-many relationship, One product has only one category but a category had many products.</p>
  
  <p>Here is what I'm trying to do in my view :</p>
  
  <pre class="lang-cs prettyprint-override"><code>@model IEnumerable&lt;MyPersonalProject.Models.Product&gt;   
  
      &lt;div id="accordion1" style="text-align:justify"&gt;
      @foreach (var category in ViewBag.Categories)
      {
          &lt;h3&gt;&lt;u&gt;@category.Name&lt;/u&gt;&lt;/h3&gt;
  
          &lt;div&gt;
  
              @foreach (var product in Model)
              {
                  if (product.CategoryID == category.CategoryID)
                  {
                      &lt;table cellpadding="5" cellspacing"5" style="border:1px solid black; width:100%;background-color:White;"&gt;
                          &lt;thead&gt;
                              &lt;tr&gt;
                                  &lt;th style="background-color:black; color:white;"&gt;
                                      @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;" })
                                      }
                                  &lt;/th&gt;
                              &lt;/tr&gt;
                          &lt;/thead&gt;
  
                          &lt;tbody&gt;
                              &lt;tr&gt;
                                  &lt;td style="background-color:White;"&gt;
                                      @product.Description
                                  &lt;/td&gt;
                              &lt;/tr&gt;
                          &lt;/tbody&gt;      
                      &lt;/table&gt;                       
                  }
              }
  
          &lt;/div&gt;
      }  
  &lt;/div&gt;
  </code></pre>
  
  <p>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.</p>
  
  <ul>
   <li>category 1</li>
    <ul><li>product 1</li><li>product 3</li></ul>
   <li>category 2</li>
    <ul><li>product 2</li><li>product 4</li></ul>
   <li>category 3</li>
    <ul><li>product 5</li></ul>
  </ul>
  
  <p>Here I will add my mapping for <strike>my one-one</strike> one-many (Thanks Brian P) relationship :</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MyPersonalProjectContext : DbContext
  {
      public DbSet&lt;Product&gt; Product { get; set; }
      public DbSet&lt;Category&gt; Category { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
  
          modelBuilder.Entity&lt;Product&gt;();
          modelBuilder.Entity&lt;Category&gt;();
      }
  }
  </code></pre>
  
  <p>I will also add my controller so you can see how I did it :</p>
  
  <pre class="lang-cs prettyprint-override"><code>public ActionResult Index()
      {
          ViewBag.Categories = db.Category.OrderBy(c =&gt; c.Name).ToList();
          return View(db.Product.Include(c =&gt; c.Category).ToList());
      }
  </code></pre>
  
  <p>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 !!!!</p>