I am writing some very poor code right now and before I even save it I was hoping to get some input on improving it. I am trying to build an html table with three cells to every row. If the collection has 5 items then that should render as two rows.
The code I have written so far I can see is not very robust and will require constant maintenance but I am unsure of other tools / methods for accomplishing the task.
<table>
@foreach (VideosModel item in Model)
{
if (cellCount == 0 || cellCount == 3)
{
@Html.Raw("<tr>")
}
<td style="padding:0px 20px;">
@item.VideoTitle <br />
<a href="@item.VideoUrl" target="_blank">
<img src="../../.../Video.jpg" /> <br />
</a>
@item.VideoDescription
<br />
</td>
cellCount++;
if (cellCount == 3 || cellCount == 6)
{
@Html.Raw("</tr>")
}
if (cellCount > 3) { cellCount = 0; }
}
</table>
You should considering using modulo instead of comparing the values of cellCount to 0, 3 or 6:
<table>
@foreach (VideosModel item in Model)
{
if (cellCount % 3 == 0)
{
@:<tr>
}
<td style="padding:0px 20px;">
@item.VideoTitle <br />
<a href="@item.VideoUrl" target="_blank">
<img src="../../.../Video.jpg" />
</a><br />
@item.VideoDescription
<br />
</td>
cellCount++;
if (cellCount % 3 == 0)
{
@:</tr>
}
}
</table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With