Peter Peter - 1 month ago 5
C# Question

How can I make C# foreach with nested subtitles?

Hello I'm a beginner and I am working with ASP.NET with MVC5.

I want to make an IEnumerable<> Food-Menu-Pricelist with nested titles (Courses) in 1 foreach so I can Create or Edit a new string (CourseName) with the CRUD-Operations I have made with Entity Framework.

I will embed a picture in the link below which I made semi/hardcoded and like you can see in the code below the titles are hardcoded.

Preview of result I want to reach:
preview of result I want to reach

I have 2 models:


  1. Course.cs = the title with Property string: CourseName

  2. Dish.cs = and the list with Property string: DishName



Is there a way I can put this table in 1 foreach in that way that I also can Edit the title with the CRUD-Operations with te model I have defined?

I already tried a lot to reach that. Anyway this was my first question on Stackoverflow..

@model IEnumerable<Project_Lameau.Models.Dish>

@{
ViewBag.Title = "Index";
}

<h2>Suggestie</h2>

<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Suggestie")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>
<hr />


<h2>Salade</h2>

<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Salade")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>
<hr />


<h2>Voorgerecht</h2>

<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Voorgerecht")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>
<hr />


<h2>Hoofd Vlees</h2>

<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Hoofd Vlees")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>
<hr />


<h2>Hoofd Vis</h2>

<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Hoofd Vis")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>
<hr />


<h2>Vega</h2>

<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Vega")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>

Rob Rob
Answer

Your top level foreach would be something like

foreach (var courseGroup in Model.GroupBy(m => m.CourseName))

And each item would be:

<h2>courseGroup.Key</h2>

<table class="table">
    @foreach (var item in courseGroup)
    {
        <tr>
            <td>@item.DishName</td>
            <td class="text-right">@item.Price</td>
        </tr>
    }
</table>

However, you can organise your data a bit more, so if you defined your classes (and tables in your database), as something like this:

public class Course
{
    public string CourseName { get; set; }
    public List<Dish> Dishes { get; set; }
}

public class Dish
{
    public string DishName { get; set; }
    public decimal Price { get; set; }
}

Then your code becomes a lot simpler:

foreach (var course in Model) //or Model.Courses - up to you

And each item would be:

<h2>course.CourseName</h2>

<table class="table">
    @foreach (var item in course.Dishes)
    {
        <tr>
            <td>@item.DishName</td>
            <td class="text-right">@item.Price</td>
        </tr>
    }
</table>