Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
@foreach (var data in ViewBag.listesite)
            {

        <tr>
            <th>@data.Title</th>           
            <td>@data.Marque </td>
            <td>@data.Model </td>
            <td>@data.Numero </td>
            <td>@data.Plage </td>
        </tr>


What I have tried:

The value of the title field does not change for each loop.
I would like the title field value to be displayed once, on a single line
Posted
Updated 5-Dec-19 2:02am
Comments
j snooze 4-Dec-19 17:26pm    
Then remove it from the for each loop and put the html just for the title above the for each loop.

1 solution

Assuming your list exposes a Count property:
Razor
@{
    int rowCount = ViewBag.listesite.Count;
    bool firstRow = true;
}
@foreach (var data in ViewBag.listesite)
{
    <tr>
        @if (firstRow)
        {
            firstRow = false;
            <th rowspan="@rowCount">@data.Title</th>
        }
        <td>@data.Marque</td>
        <td>@data.Model</td>
        <td>@data.Numero</td>
        <td>@data.Plage</td>
    </tr>
}

EDIT: If there are multiple titles in the list, then group the records:
Razor
@{
    var allItems = (IEnumerable<YourType>)ViewBag.listesite;
}
@foreach (var group in allItems.GroupBy(i => i.Title))
{
    int rowCount = group.Count();
    bool firstRow = true;
    
    foreach (var data in group)
    {
        <tr>
            @if (firstRow)
            {
                firstRow = false;
                <th rowspan="@rowCount">@data.Title</th>
            }
            <td>@data.Marque</td>
            <td>@data.Model</td>
            <td>@data.Numero</td>
            <td>@data.Plage</td>
        </tr>
    }
}
NB: You'll need to cast the list to a concrete type, since extension methods can't be invoked on dynamic values.
 
Share this answer
 
v3
Comments
Member 14663996 6-Dec-19 8:36am    
your solution works when there is one title for all records. But there is a title for each group of records
Richard Deeming 6-Dec-19 8:37am    
Then you'll need to group the records by title.
Member 14663996 6-Dec-19 9:15am    
in the controller or in the view ?
Richard Deeming 6-Dec-19 9:17am    
I've updated my solution with an example of doing it in the view, but you could do it in the controller if you wanted to.
Member 14663996 6-Dec-19 9:58am    
thank.
YourType est class or table ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900