title: Dynamically generate table in loop / foreach MVC view
body: <p>I am writing some very poor code right now and before I even save it I was hoping to get some input on improving it.  I am trying to build an html table with three cells to every row. If the collection has 5 items then that should render as two rows.</p>
  
  <p>The code I have written so far I can see is not very robust and will require constant maintenance but I am unsure of other tools / methods for accomplishing the task.</p>
  
  <pre><code>&lt;table&gt;
      @foreach (VideosModel item in Model)
      {
          if (cellCount == 0 || cellCount == 3)
          { 
              @Html.Raw("&lt;tr&gt;") 
          }
             &lt;td style="padding:0px 20px;"&gt;
                  @item.VideoTitle &lt;br /&gt;
                  &lt;a href="@item.VideoUrl" target="_blank"&gt;
                      &lt;img src="../../.../Video.jpg" /&gt; &lt;br /&gt;
                  &lt;/a&gt;
                  @item.VideoDescription
                  &lt;br /&gt;
              &lt;/td&gt;
  
          cellCount++;
  
          if (cellCount == 3 || cellCount == 6)
          { 
              @Html.Raw("&lt;/tr&gt;") 
          }
  
          if (cellCount &gt; 3) { cellCount = 0; }
      }
  &lt;/table&gt;
  </code></pre>