title: Passing multiple result sets to a view from a controller in ASP.NET MVC?
body: <p>So I have a controller set up as follows:</p>
  
  <pre><code>using NonStockSystem.Models;
  
  namespace NonStockSystem.Controllers
  {
      [Authorize(Users = "DOMAIN\\rburke")]
      public class AdminController : Controller
      {    
          private NonStockSystemDataContext db = new NonStockSystemDataContext();
  
          public ActionResult Index()
          {
              var enumProducts = from p in db.Products select p;
              ViewData["Title"] = "Administration";
              return View(enumProducts.ToList());
          }
      }
  }
  </code></pre>
  
  <p>The Index view on the Admin controller just lists the products in the system and allows us to click on a product to view / edit / delete it. Really simple. However each product has a CategoryID which tell us which Category it is in which is stored in a separate table.</p>
  
  <p>The (very simplified) current view is this:</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="NonStockSystem.Views.Home.Admin" %&gt;
  &lt;%@ Import Namespace="NonStockSystem.Models" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"&gt;
  
  &lt;%
  foreach (Product p in (IEnumerable)ViewData.Model)
  { %&gt;
  
      &lt;%=p.Name.ToString() %&gt; (&lt;a href="/Admin/Edit/&lt;%=p.ID.ToString() %&gt;"&gt;Edit&lt;/a&gt; - &lt;a href="/Admin/Delete/&lt;%=p.ID.ToString() %&gt;"&gt;Delete&lt;/a&gt;)&lt;br /&gt;
  
  &lt;%
  } %&gt;   
  
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>This is fine at the moment as there are only 10 or 15 products in the system whilst I develop and test it however once I deploy it there will be approx. 300 products in the database. I'm fine with displaying them all on one page however I'd like to use (a href="#category") links much like Wikipedia does so at the top of the page I can have the list of categories and when you click one it brings you to the appropriate section of the page. So, my view in that case will look like so:</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="NonStockSystem.Views.Home.Admin" %&gt;
  &lt;%@ Import Namespace="NonStockSystem.Models" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"&gt;
  
  &lt;ul&gt;
  &lt;%
  foreach (Category c in (IEnumerable)ViewData.Model)
  { %&gt;
      &lt;li&gt;&lt;a href="#&lt;%=c.Name.ToString() %&gt;"&gt;&lt;%=c.Name.ToString() %&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;%
  } %&gt;
  &lt;/ul&gt;
  
  &lt;hr /&gt;
  
  &lt;%
  foreach (Category c in (IEnumerable)ViewData.Model)
  { %&gt;
      &lt;% // Display the category name above all products from that category %&gt;
      &lt;h2&gt;&lt;a name="&lt;%=c.Name.ToString() %&gt;"&gt;&lt;%=c.Name.ToString() %&gt;&lt;/a&gt;&lt;/h2&gt;
  
      &lt;% // Need to limit the following foreach to grab only products in this category
      foreach (Product p in (IEnumerable)ViewData.Model)
      { %&gt;
          &lt;%=p.Name.ToString() %&gt; (&lt;a href="/Admin/Edit/&lt;%=p.ID.ToString() %&gt;"&gt;Edit&lt;/a&gt; - &lt;a href="/Admin/Delete/&lt;%=p.ID.ToString() %&gt;"&gt;Delete&lt;/a&gt;)&lt;br /&gt;
      &lt;%
      } %&gt;
  } %&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Firstly, I'm not entirely sure this is the "right" way to do this so I'm definitely open to suggestions of a different way of doing things but if this is the way to go then I need to know how to (1) pass two result sets to the view (Products and Categories) and (2) loop through a subset of the Products in each foreach loop grabbing only the ones in the appropriate category?</p>
  
  <p>Cheers!</p>