Foreach loop for tables in MVC4

前端 未结 2 1057
南笙
南笙 2021-01-15 23:12

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.

       
相关标签:
2条回答
  • 2021-01-15 23:38

    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>
    

    Using divs you would...

        @foreach(var item in Model)
         {
             <div style="float:left;">Rtn. @item.Mem_NA<br />(@item.Mem_Occ)</div>
         }
    
    0 讨论(0)
  • 2021-01-15 23:38

    Thank you everybody i got another way

    @{
     int total = 0; 
    }
    <table id="memberlist">
    <tbody>
        @foreach(var item in Model)
        {
            if( total % 4 == 0 ){
            @:<tr>
            }
            <td>Rtn. @item.Mem_NA<br />(@item.Mem_Occ)</td>
            if( total+1 % 5 == 0 ){
            @:</tr>
            }
            total++;
        }
    </tbody>
    

    0 讨论(0)
提交回复
热议问题