Is there any way, in ASP.Net MVC, to condense the following code to a single foreach loop?
<table class="table">
<tr>
<td>
Name
</td>
<%
foreach (var item in Model)
{
%>
<td>
<%= item.Name %>
</td>
<%
}
%>
</tr>
<tr>
<td>
Item
</td>
<%
foreach (var item in Model)
{
%>
<td>
<%= item.Company %>
</td>
<%
}
%>
</tr>
</table>
Where model is an IEnumerable<SomeObject>
:
public class SomeObject
{
public virtual Name {get;set;}
public virtual Company {get;set;}
}
This would output a table as follows:
Name | Bob | Sam | Bill | Steve |
Company | Builder | Fireman | MS | Apple |
I know I could probably use an extension method to write out each row, but is it possible to build all rows using a single iteration over the model?
This is a follow on from this question as I'm unhappy with my accepted answer and cannot believe I've provided the best solution.