So I have a controller set up as follows:
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());
}
}
}
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.
The (very simplified) current view is this:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="NonStockSystem.Views.Home.Admin" %>
<%@ Import Namespace="NonStockSystem.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<%
foreach (Product p in (IEnumerable)ViewData.Model)
{ %>
<%=p.Name.ToString() %> (<a href="/Admin/Edit/<%=p.ID.ToString() %>">Edit</a> - <a href="/Admin/Delete/<%=p.ID.ToString() %>">Delete</a>)<br />
<%
} %>
</asp:Content>
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:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="NonStockSystem.Views.Home.Admin" %>
<%@ Import Namespace="NonStockSystem.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<ul>
<%
foreach (Category c in (IEnumerable)ViewData.Model)
{ %>
<li><a href="#<%=c.Name.ToString() %>"><%=c.Name.ToString() %></a></li>
<%
} %>
</ul>
<hr />
<%
foreach (Category c in (IEnumerable)ViewData.Model)
{ %>
<% // Display the category name above all products from that category %>
<h2><a name="<%=c.Name.ToString() %>"><%=c.Name.ToString() %></a></h2>
<% // Need to limit the following foreach to grab only products in this category
foreach (Product p in (IEnumerable)ViewData.Model)
{ %>
<%=p.Name.ToString() %> (<a href="/Admin/Edit/<%=p.ID.ToString() %>">Edit</a> - <a href="/Admin/Delete/<%=p.ID.ToString() %>">Delete</a>)<br />
<%
} %>
} %>
</asp:Content>
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?
Cheers!