Reputation: 2257
I need to create a table which looks like:
S1 S2 S3 S4 S5
C1 C1S1 C1S2 C1S3 C1S4 C1S5
C2 C2S1 C2S2 C2S3 C2S4 C2S5
C3 C3S1 C3S2 C3S3 C2S4 C3S5
C4 C4S1 C4S2 C4S3 C3S4 C4S5
C5 C5S1 C5S2 C5S3 C4S4 C5S5
but following code:
<table>
@foreach (var client in this.Model.Clients)
{
<tr>
<td >@client.Name</td>
@foreach (var session in this.Model.Sessions)
{
foreach (var data in this.Model.ClientSessions)
{
if (data.ClientId== client.ClientId&& data.SessionId == session.SessionId)
{
<td>@data.TotalClientSessions</td>
}
}
}
</tr>
}
</table>
produce:
//here should be missing header
C1 C1S1 C1S2 C1S3 C1S4 C1S5
C2 C2S1 C2S2 C2S3 C2S4 C2S5
C3 C3S1 C3S2 C3S3 C2S4 C3S5
C4 C4S1 C4S2 C4S3 C3S4 C4S5
C5 C5S1 C5S2 C5S3 C4S4 C5S5
I don't know how to smartly modify this code to get headers. I would add another foreach at the bottom to create header lines this way:
<tr>
<th></th>
@foreach (var session in this.Model.Sessions)
{
<th>@Session.Id</th>
}
</tr>
but I believe, that there is other, more smarter solution to achieve that. Any help will be appreciated.
Upvotes: 0
Views: 1800
Reputation: 56688
Since HTML tables are rendered row-by-row, not column by column, you have to iterate over clients, and thus there is no way to produce a header row as a part of this loop. Without ugly hacks that is. Because you could potentially insert a dummy client in the list that would signify the header row, but that sounds really ugly and I would not recommend going this way.
You proposed solution with a separate foreach over sessions looks totally fine, optimal and readable to me. I would approach it the same exact way.
Upvotes: 1