views:

44

answers:

1

I have a List(Name, Item, Group#) that I would like to display in a table. How do I display this data with all the names in the 1st row, item in the 2nd, and group number in the 3rd row.

Name: | Jon | Tom | Kate | Brian |

Item: | Cup | Hat | Door | Store |

Group#:| 2 | 8 | 10 | 154 |

+2  A: 

Because you're going horizontally instead of vertically, this makes the code example I've thought up a bit messy. I'm not sure if there's an alternative to this, but this should work.

<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.Item %>
            </td>
        <% 
        } 
        %>
    </tr>

    <tr>
        <td>
            Group
        </td>    
        <% 
        foreach (var item in Model) 
        {
         %>
            <td>
                <%= item.Group%>
            </td>
        <% 
        } 
        %>
    </tr>

</table>
GenericTypeTea
I usually use the foreach vertically, I was hoping there would be a slicker alternative, but I also didnt think about this one. Thanks!!!
An extension method would probably be the nicest way to do this. But the method would basically be doing this anyway.
GenericTypeTea
Could you do this using just a single foreach?
Robin Robinson
@Robin - I've asked the question myself here: http://stackoverflow.com/questions/3012326/how-can-i-create-a-horizontal-table-in-a-single-foreach-loop-in-mvc
GenericTypeTea