views:

112

answers:

2

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.

A: 

Check the following mat it help you

<table class="table">
    <tr>
        <td>
            <table>
                <tr><td>Name</td></tr>
                <tr><td>Item</td></tr>
             <table>
        </td>    
        <% 
        foreach (var item in Model) 
        {
         %>
            <td>
                <table>
                  <tr><td><%= item.Name %></td></tr>
                  <tr><td><%= item.company %></td></tr>
                <table>
            </td>
        <% 
        } 
        %>
    </tr>
</table>

Note:-

No check though :)
Salil
@Salil - Heh, clever. I like it, but I feel dirty for liking it. I think I'd prefer my answer over a table>in>table solution, but smart thinking!
GenericTypeTea
@GenericTypeTea :- yeah! i agree with u it's dirty. i also don't mind 2 loops over table>in>table solution. But it's just 4 record. Cheer's
Salil
A: 

If you are not restricted to using pure tables, then this would work for you.

<table class="table"> 
    <tr> 
        <th> 
             Name<br/>
             Item<br/>
        </th>     
        <%  
        foreach (var item in Model)  
        { 
         %> 
            <td> 
                  <%= Html.Encode(item.Name) %><br/>
                  <%= Html.Encode(item.company) %><br/>
            </td> 
        <%  
        }  
        %> 
    </tr> 
</table> 

You can definitely improve this by using span and css stylings.

Syd
Is it legitimate to put a table header within a table row? Anyway, could probably just set the th to a td anyway. Quite a nice example. Must nicer than tables within tables anyway!
GenericTypeTea
@GeneticTypeTea, it definitely works. Tested on both IE8 and Firefox. Some advantages over "table within tables" are :1) less bandwidth. 2) easier to style with css.
Syd