I have a multi-level dropdown menu on my MVC Application and I’m trying to sort the products by brand, gender and type. My menu looks like this:
<li>Man
<ul>
<li><h2>By Type</h2></li>
<li>CATEGORY</li>
</ul>
<ul>
<li><h2>By Brand</h2></li>
<li>BRAND</li>
</ul>
</div>
</li>
<li>
<li>Woman
<ul>
<li><h2>Categories</h2></li>
<li>CATEGORY</li>
</ul>
<ul>
<li><h2>Brands</h2></li>
<li>BRAND</li>
</ul>
</div>
</li>
<li>
and here is one of my controllers:
public ActionResult Man(int type)
{
var productTypeModel = storeDB.Categories.Include("Products")
.Single(g => g.CategoryId == type);
return View(productTypeModel);
}
This is the partialview that loads into the menu:
@model IEnumerable<Store.Models.Category>
<ul>
<li><h2>By Type</h2></li>
@foreach (var type in Model)
{
<li>@Html.ActionLink(type.Name,
"Man", "Store",
new { Category = type.CategoryId }, null)
</li>
}
</ul>
Is there any way that I can display only the categories and brands that have products associated with each gender using lambda expressions? I mean, I don’t want “skirt” to appear on “By Type” when I’m browsing products for man and I don’t want a brand that has products for man appearing on the ladies section.
You could use GroupBy:
And then in the template:
This would produce something like: