tags:

views:

39

answers:

3

Right now I have this code which enumerates to a singe column table all the way down the page.

Is there a way to make it two colums next to each other all the way to the end?

 <table>
<% foreach (var item in Model)
   { %>
   <tr>
        <td>
          <%=Html.Encode(item.PartNo)%>
       </td>
   </tr>

<% } %>

</table>
+1  A: 

You can just change the foreach loop to a for loop for more flexibility:

<% var itemList = Model.ToList(); %>

<table>
<% for (int i=0; i <= itemList.Count; i+=2))
   { %>
   <tr>
        <td>
          <%= Html.Encode(itemList[i].PartNo) %>
       </td>
       <td>
          <% if (i+1 < itemList.Count) 
             { 
                 Response.Write(Html.Encode(itemList[i+1].PartNo));
             }
           %>
       </td>
   </tr>
<% } %>
</table>

If you're doing this a lot, you could consider turning it into an HTML helper to output any number of columns.

womp
Ah yes, thank you! I get in the habit of using a foreach and forget about using anything else!!Thanks!
riverdayz
I get an error on the i<= Model.Count saying operator <= cannot be applied to opperands of type int and method group
riverdayz
It should be Model.Count()
rtalbot
ok fixed that now I get an error on the <%=Html.Encode(Model[i].PartNo) %>" saying cannot apply indexing to an expression of type System.Collections.Generic.IENumerable<Item>
riverdayz
@riverdayz - you'll need to convert your IEnumerable to a List in order to use indexes and Count on it. I've updated my code for you.
womp
thanks Womp, I appreciate your help here!
riverdayz
A: 

If you're just looking for an extra column, you can keep the foreach and just add a <td></td>:

<table> 
<% foreach (var item in Model) 
   { %> 
   <tr> 
        <td> 
          <%=Html.Encode(item.PartNo)%> 
       </td> 
       <td>
         <%=Html.Encode(item.NextPartNo)%> 
       </td>
   </tr> 

<% } %> 

rtalbot
the extra column needs to have the next item.partno in it. not just a blank column
riverdayz
you should add this additional requirement to the question then
rtalbot
others have understood just fine thanks
riverdayz
well womp did (good job womp!). all the rest of the answers in this thread are doing something different - which is a pretty good argument for providing clarification to the question but whatever.
rtalbot
A: 

Building off of womp's answer, here's an example (untested!) that works using an enumerator and can display an arbitrary number of columns. This may address some of the issues you're mentioning as well.

<table><% 
    int numberOfColumns = 2;
    var enumerator = Model.GetEnumerator();
    bool endOfEnumerator = !enumerator.MoveNext();

    while(!endOfEnumerator)
    {   %>
    <tr><%
        for (int i = 0; i < numberOfColumns; i++)
        { %>
        <td>
            <%= Html.Encode(enumerator.Current.PartNo) %>
        </td><%
            if (!enumerator.MoveNext())
            {
                endOfEnumerator = true;
                break;
            }
        } %>
    </tr><%
    } %>
</table>
Dr. Wily's Apprentice