title: ASP.NET MVC 3 Razor Nested foreach with if statements
body: <p>I am a junior web developer and this is my first time posting here.</p>
  
  <p>I am having a problem where for some reason the most inner if statement is not recognized and is instead printed as text. Anyone got a solution for this?</p>
  
  <p>EDIT: Found a solution: <a href="https://stackoverflow.com/questions/5733885/cant-seem-to-conditionally-create-a-new-table-row-using-razors-foreach-and-if">Can&#39;t seem to conditionally create a new table row using Razor&#39;s foreach and if statements?</a></p>
  
  <pre><code>@model IEnumerable&lt;FairShare.Models.Product&gt;
  @{
      ViewBag.Title = "Products";
  }
  &lt;h2&gt;
      Auctions&lt;/h2&gt;
  &lt;table border="1"&gt;
      &lt;col width="192" /&gt;
  
      @{int i = 0;}
          @foreach (var item in Model)
          {
              if (item.DateEnd.Subtract(DateTime.Now).TotalMinutes &gt; -5)
              {
                  if (i == 0)
                  {
                      &lt;tr&gt;
                  }
              &lt;td&gt;
                  &lt;a href="/ProductDetails/Index?productID=@item.ID"&gt;
                      &lt;img src="Images/@item.ImageName" width="192" height="108"/&gt;
                  &lt;/a&gt;
                  &lt;br /&gt;
                  &lt;a href="/ProductDetails/Index?productID=@item.ID"&gt;@Html.DisplayFor(modelItem =&gt; item.ShortTitle)&lt;/a&gt;&lt;br /&gt;
                  @Html.DisplayFor(modelItem =&gt; item.ShortDescription)&lt;br /&gt;
                  &lt;div style="color: red;"&gt;@Html.DisplayFor(modelItem =&gt; item.TimeLeft)&lt;/div&gt;
                  &lt;div style="color: green;"&gt;
                      Current bid: @Html.DisplayFor(modelItem =&gt; item.CurrentBid)&lt;/div&gt;
              &lt;/td&gt;
  
                  i = i + 1;
                  if (i == 5)
                  {
                      &lt;/tr&gt;
                      i = 0;
                  }
              }
          }
  
  &lt;/table&gt;
  </code></pre>