Reputation: 33
I've been working on an ASP.net MVC project and currently I am pulling data from a database and displaying them in alternating rows. So the display of data will be as follows.
And so on. I have managed to do this correctly with the following code. To me this seems pretty inefficient and I am asking is if there is a much simpler way to do this. Thanks.
@{
@:<table>
int modcheck = 0;
foreach (var item in @Model)
{
if(modcheck % 2 == 0 )
{
@:<tr><td style="width:400px">
<h3>@item.Name</h3>
@:</td>
}
else
{
@:<td style="width:400px">
<h3>@item.Name</h3>
@:</td></tr>
}
modcheck++;
}
@:</table>
}
Upvotes: 3
Views: 42
Reputation: 1291
Instead of doing a foreach loop you could do a for loop and increment by two, something like:
for (int x=0; x < @Model.Lenght; x += 2)
{
@:<tr>
@:<td style="width: 400px"><h3>@Model[x]</h3></td>
@:<td style="width: 400px"><h3>@Model[x+1]</h3></td>
@:</tr>
}
My ASP is a little rusty and this probably wont compile, but it should get you going.
Upvotes: 3