tags:

views:

301

answers:

2

I would like to mimic the RepeatColumn function of the ASP.NET DataList control in ASP.NET MVC. The goal is to have 2 or more rows of a collection in one table row.

The following code works, but it looks very ugly to me. Is there better way to do it? Thanks for any help!

<table>
    <% 
    for (int i = 0; i < items.Count(); i++)
    {
    %>
    <tr>
        <% for (int j = 0; j < 2; j++)
           {
               if (i + j < items.Count())
               {
                   var item = items[i + j];
                   %>
                   <td>
                   <% Html.Encode(item.title) %>
                   </td>
                   <%
               }
               else 
               {
                %>
                    <td>&nbsp;</td>
                <%
               }
           }%>            
    </tr>
    <%} %>
</table>
A: 

Check out the DataList helper in MVCContrib.

Mauricio Scheffer
thank you very much, that helps!
Joris
A: 

try using datalist control. <% dtList.DataSource = ViewData.Model; dtList.DataBind(); %>

... your data goes here. <%# DataBinder.Eval(Container.DataItem, "ProductName")%>

henryt1975