Grouping Orders By Category In Mvc4 View
In our MVC4 application my database has an Orders table, a Products table and a Category table. Every Order has a product (plus quantity) in a foreign key relationship and the prod
Solution 1:
OKay, we solved the problem by making a tuple:
publicActionResultIndex()
{
var orders = db.Orders.Include(o => o.Product)
.GroupBy(o => o.Product.Category.Name)
.Select(cat => cat.FirstOrDefault());
var products = db.Orders.Include(o => o.Product);
returnView(Tuple.Create(orders.ToList(), products.ToList()));
}
and in the view:
@foreach (var item inModel.Item1) {
<table><tr><td>
@Html.DisplayFor(catName => item.Product.Category.Name)
<br />
@foreach (var product in Model.Item2)
{
if (product.Product.Category.Name == item.Product.Category.Name)
{
@Html.DisplayFor(productName => product.Product.Name)
@Html.DisplayFor(productName => product.Quantity) <br />
}
}
</td></tr></table>
}
Looks a bit crappy as of now but it works!
Post a Comment for "Grouping Orders By Category In Mvc4 View"