foreach循环在MVC4表 [英] Foreach loop for tables in MVC4

查看:860
本文介绍了foreach循环在MVC4表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#在做MVC4我的项目。我在我的模型一个IEnumerable列表。我用下面的循环列出,在我的观点。

I am doing my project in MVC4 using c#. I have an IEnumerable list in my model. I use the following loop to list that in my view.

       <table id="memberlist">
       <tbody>
        @foreach(var item in Model)
         {
          <tr>
             <td>Rtn. @item.Mem_NA<br />(@item.Mem_Occ)</td>
          </tr>
         }
       </tbody>
       </table>

其实我想下表中的输出如

Actually i want the output like in the following table

 <table id="memberlist">
       <tbody>
          <tr>
             <td>Rtn.item.Mem_NA1<br />(item.Mem_Occ1)</td>
             <td>Rtn.item.Mem_NA2<br />(item.Mem_Occ2)</td>
             <td>Rtn.item.Mem_NA3<br />(item.Mem_Occ3)</td>
          </tr>
          <tr>
             <td>Rtn.item.Mem_NA4<br />(item.Mem_Occ4)</td>
             <td>Rtn.item.Mem_NA5<br />(item.Mem_Occ5)</td>
             <td>Rtn.item.Mem_NA6<br />(item.Mem_Occ6)</td>
          </tr>
       </tbody>
       </table>

这有可能产生类似上面使用foreach循环的表。或者更好的使用表的DIV来代替。请帮我

Is that possible for generating a table like above using a foreach loop. or is better using the div instead of table. Please help me

推荐答案

首先,它是完全有可能做到这一点。其次,你不应该这样做。考虑使用&LT; D​​IV /&GT; 来使布局流。 表与创建的div将这时窗口调整大小。

Firstly it is entirely possible to do this. Secondly, you should not do this. Consider using <div /> to make the layout "flow". The "table" created with divs will resize with the window then.

       @{
           int groupings = 3;
           var grouped = Model.Select((x,i) => new { x, i = i / groupings  })
                         .GroupBy(x => x.i, x => x.x);
       }

       <table id="memberlist">
       <tbody>
        @foreach(var items in grouped)
         {
          <tr>
             @foreach(var item in items)
             {
                 <td>Rtn. @item.Mem_NA<br />(@item.Mem_Occ)</td>
             }
          </tr>
         }
       </tbody>
       </table>

使用div的你会...

Using divs you would...

    @foreach(var item in Model)
     {
         <div style="float:left;">Rtn. @item.Mem_NA<br />(@item.Mem_Occ)</div>
     }

这篇关于foreach循环在MVC4表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆