0

嗨,我需要帮助将内容并排放置。

现在我可以在左侧和右侧分别显示内容。但我需要显示数据库中的记录。为此,我正在使用 foreach 循环。所以我需要显示的是左侧的第一条记录和右侧的第二条记录和左侧的第三条记录,依此类推。我怎么能像这样循环。

我被困在这里。我需要帮助..

我正在使用此代码,但正如我之前所说的那样,所有内容都在显示。

   <body>
   @foreach (var item in Model)

        {
  <!-- Main Table(You can define padding accordingly) -->
    <table width="80%" border="0" cellspacing="0" cellpadding="5">
  <tr>
  <td width="50%">

   <!-- Table on left side -->
  <table width="100%" border="0" cellspacing="0" cellpadding="1">
    <tr>
  <td>
  <span class="Title">APP</span>
  <br/>
    <div class="listRowA" onmouseover="this.className='listRowAHover'" onmouseout="this.className='listRowA'">
    <div class="listPageCol"  onclick="location.href='../Home/ControlPanel'">
    <span class="listlCol1">

          <br />
             @item.ID
            </span>
      <span class="listCol2"><img src="../Content/Images/20_thumb.jpg" width="281" height="200" border="0" alt="image"  /></span>


         </div>
        </div>

      </td>

      </tr>

    <tr>
    <td>&nbsp;</td>
   <td></td>
   <td>&nbsp;</td>
   </tr>
    <tr>
   <td>&nbsp;</td>
   <td> </td>
   <td>&nbsp;</td>
  </tr>
  </table>
  <!-- END -->
  </td>
  <td width="50%">

  <!-- Table on right side -->
  <table width="100%" border="0" cellspacing="0" cellpadding="1">
  <tr>

   <td>
  <div class="listRowA" onmouseover="this.className='listRowAHover'"  onmouseout="this.className='listRowA'">
    <span class="listCol2"><img src="../Content/Images/20_thumb.jpg" width="281"  height="200" border="0" alt="image"  /></span></div>
    </td>
     </tr>
   <tr>
  <td>&nbsp;</td>
  <td></td>
   <td>&nbsp;</td>
  </tr>
   <tr>
  <td>&nbsp;</td>
  <td> </td>
 <td>&nbsp;</td>
   </tr>
 </table>
  <!-- END -->
   </td>
  </table>

<!-- END OF MAIN TABLE -->
    }
 </body>
4

2 回答 2

1

使用此模式,您可以创建任意数量的列。如果您使用的是 html 表格,则还需要测试当总集合大小不能被columns.

@{
    int columns = 3;
}

@for (int i = 0; i < Model.Count; i++)
{
    var currentModel = Model[i];

    int col = i % columns;

    if (col == 0)
    {
        // left
    }
    else if (col == 1)
    {
        // middle
    }
    else  // col == 2
    {
        // right
    }
}
于 2013-09-12T23:59:05.003 回答
1

代替

@foreach (var item in Model)

利用

@for (var i=0; i < Model.Count; i+=2)
{

}

并访问模型的属性,例如

 @Model[i].ID

为了确定哪个应该向右哪个应该向左,您可以使用

@Model[i].ID for left 

@if(i+1 < Model.Count)
{
  Model[i+1].ID for right
}

只要确保i+1永远不会超过Model.Count

于 2013-09-12T02:13:42.683 回答