Splitting foreach in a table

2019-01-29 13:35发布

问题:

As of right now my table looks like this:

But I want to split the foreach to have it look something like this

Is possible to do this?

And this is the foreach:

@foreach (var date in ViewBag.MissingDays)
{
  var isoDate = date.ToString("yy-MM-dd");
  <tr>
    <td>
      <a href="javascript:SetDate('@isoDate');">@isoDate</a>
    </td>
  </tr>
}

回答1:

You can use the following code to generate rows containing 9 columns

@{var counter = 1; }
<tr>
  @foreach (var date in ViewBag.MissingDays)
  {
    var isoDate = date.ToString("yy-MM-dd");
    <td><a href="javascript:SetDate('@isoDate');">@isoDate</a></td>
    if (counter % 9 == 0)
    {
      @:</tr><tr> // end current row and begin new one
    }
    counter++;
  }
</tr>