A very old thread, but ...
I used the following (but was looking for a better approach when I came across this question)
Create a block of code that moves around the objects and creates HTML for each line in the variable, then outputs the variable.
Plus: you only loop once and don't have nested tables (so things are likely to line up), but the minus is html string manipulation.
It is likely to use stringbuilder instead of string, but the principle is the same.
@code Dim tr1 As String = "<th>Name</th>" Dim tr2 As String = "<th>Company</th>" For Each thingy As object In Model tr1 += "<th>" & thingy.Name & "</th>" tr2 += "<th>" & thingy.Company & "</th>" Next End Code @<table class="table table-condensed"> <tr>@Html.Raw(tr1)</tr> <tr>@Html.Raw(tr2)</tr> </table>
source share