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 product belongs to a category with a foreign key.
So Order has an OrderId, ProductId (FK to Product), a data plus a quantity and Product has a ProductId, Name, and a CategoryId (FK to Category) and Category has a CategoryId and a Name.
Now we made a "shopping list" which displays the categories that are ordered during the day and display the products below the category name.
To only display the products of this date we are using the foreach loop below:
@model Tuple<List<myproject.Models.Order>, List<myproject.Models.Order>>
@foreach (var item1 in Model.Item1)
{
if (item1.Date.ToString("d").Equals(DateTime.UtcNow.Date.ToString("d")))
{
<tr>
<td>
<h3>@Html.DisplayFor(catName => item1.Product.Category.Name)</h3>
</td>
</tr>
}
foreach (var item2 in Model.Item2)
{
if (item2.Product.Category.Name.Equals(item1.Product.Category.Name) &&
item2.Date.ToString("d").Equals(DateTime.UtcNow.Date.ToString("d")))
{
<tr>
<td>
@Html.DisplayFor(productName => item2.Product.Name)
 (x @Html.DisplayFor(quantity => item2.Quantity))
</td>
</tr>
}
}
}
to display categories and the products in the category it misses the first name of the category in the list. The output is:
Apple (x 1)
Banana (x 1)
Snacks
Fries (x 3)
Hamburger(x 1)
Veggies
Tomato (x 1)
instead of
Fruit
Apple (x 1)
Banana (x 1)
Snacks
Fries (x 3)
Hamburger(x 1)
Veggies
Tomato (x 1)
I've been breaking my head on this for the past hour. Can someone explain why it is not displaying the entry for "fruit"?