开发者

Display Records Horizontally ASP.NET MVC

开发者 https://www.devze.com 2023-02-20 07:33 出处:网络
Essentially, what I\'m trying to do is build a table of data dynamically on a page that is 3 columns wide by however many long.

Essentially, what I'm trying to do is build a table of data dynamically on a page that is 3 columns wide by however many long.

Working off a Classic ASP example, I've gotten as far as trying that, but not all the records display and they still render on the page as rows instead of columns.

Here's my failed code:

<table width="750" cellpadding="0" cellspacing="0" border="0">
<%
Dim recCount As Integer = 0
For Each a In Model.Templates
    If recCount Mod 3 = 0 Then
        If recCount <> 0 Then
            Response.Write("</tr>")
            Response.Write("<tr><td>template found!</td>")
        Else
            Response.Write("<td>template found!</td>")
        End If
    End If
    recCount = recCount + 1
Next
%>
</table>

I'm not overly certain how to do it, and I'm fairly sure it's probably really simple to do on a view page.

My other option is to set a finite amount of records to be created and build the table on the page from that, but I'd much rather do it dynamically.

开发者_开发知识库I guess also an alternative option would be to use a repeater control? Though I don't know if this is a control that will work with MVC.

Thanks in advance for any help.


Think I got what you meant. You want to display 3 records per row in your table. Sorry for my lousy VB:

<table...>
<% var templatesCount = Model.Templates.Count %>
<% For i = 0 To templatesCount - 1 Step 3 %>
  <tr>
  <% For j = 0 to 2 %>
    <td>
    <% If i + j < templatesCount %>
      Template Found!
    <% End If %>
  <% Next j %>
    </td>
  </tr>
<% Next i %>
</table>


If you want to use some HtmlHelpers you could search for MvcContrib project - it has Html.Grid extension method.

Or in mvc 3 you could use Html.WebGrid method.

Or here is code on asp.net mvc.

 <table>
     <tr>
         <% int count = items.Count();
            int additionalCount = count % 3 == 0 ? 0 : 1;
            int countPerColumn = count / 3 + additionalCount; 
            int i = 0;
            foreach(var item in items) 
          { 
             if (i % countPerColumn == 0 && i != 0)  
             {%> 
                 </tr><tr>
          <% } %>
               <td><%= item.Name %></td>
         <% i++; 
           } %>    
    </tr>
 </table>

sorry, i don't know VB.NET.


It's hard to know exactly what you want from this, and as you're not providing output i'll just give some tips.

I'd recommend doing the following, as it'll make your life a little easier, your code a litte cleaner and debugging should be a bit simpler:

  • write a Helper method which will do this logic for you, that way you don't clutter up your view (http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs)
  • do not use Response.Write as this really is harking back to classic ASP days, instead return a string from your Helper method.

you can very easily step through your helper method then, and sanity-check the string that is being returned.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号