title: Seems like a bug in MVC 4 Razor "for" loop?
body: <p>I am writting an MVC4 using Microsoft ASP.NET MVC 4 version 4.0.30.506.0 and I have a for loop inside a razor page that generates an stupid runtime error!</p>
  
  <p>This is the code I wrote:</p>
  
  <pre><code>@{
                      if (Model.IsAuction)
                      {
                          &lt;li class="bc49"&gt;@Html.ActionLink("ABCD", "Search", "Home", new {city = "all", type = "auction", category = "all", keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                      }
                      else
                      {
                          &lt;li class="bc49"&gt;@Html.ActionLink("EFGH", "Search", "Home", new {city = "all", type = "product", category = "all", keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                      }
  
                      int crumpsCount = 48;
  
                      for (int i = 0; i &lt; Model.Categories.Count; i++)
                      {
                          className = "bc" + crumpsCount;
  
                          if (Model.IsAuction)
                          {
                              &lt;li class="@className"&gt;@Html.ActionLink(Model.Categories[i].Title, "Search", "Home", new {city = "all", type = "auction", category = Model.Categories[i].Name, keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                          }
                          else
                          {
                              &lt;li class="@className"&gt;@Html.ActionLink(Model.Categories[i].Title, "Search", "Home", new {city = "all", type = "product", category = Model.Categories[i].Name, keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                          }
  
                          crumpsCount--;
                      }
                  }
  </code></pre>
  
  <p>And here is the error generated by Visual Studio 2012:</p>
  
  <p><img src="https://i.stack.imgur.com/IPBqo.png" alt="Error generated!"></p>
  
  <p>The error happens after the loop is finished and i's value is equal to Model.Categories.Count. Seems like the compiler tries to access the Model.Categories[Model.Categories.Count] value!</p>
  
  <p>The funny fact is that, when I replace this code with foreach loop or even if I add a foreach loop right after the for loop, the code executes correctly and pass over the for loop!</p>
  
  <p>This is the code that works:</p>
  
  <pre><code>@{
                      if (Model.IsAuction)
                      {
                          &lt;li class="bc49"&gt;@Html.ActionLink("ABCD", "Search", "Home", new {city = "all", type = "auction", category = "all", keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                      }
                      else
                      {
                          &lt;li class="bc49"&gt;@Html.ActionLink("EFGH", "Search", "Home", new {city = "all", type = "product", category = "all", keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                      }
  
                      int crumpsCount = 48;
  
                      for (int i = 0; i &lt; Model.Categories.Count; i++)
                      {
                          className = "bc" + crumpsCount;
  
                          if (Model.IsAuction)
                          {
                              &lt;li class="@className"&gt;@Html.ActionLink(Model.Categories[i].Title, "Search", "Home", new {city = "all", type = "auction", category = Model.Categories[i].Name, keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                          }
                          else
                          {
                              &lt;li class="@className"&gt;@Html.ActionLink(Model.Categories[i].Title, "Search", "Home", new {city = "all", type = "product", category = Model.Categories[i].Name, keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                          }
  
                          crumpsCount--;
                      }
  
                      foreach (CategoryInfo categoryInfo in Model.Categories)
                      {
                          className = "bc" + crumpsCount;
  
                          if (Model.IsAuction)
                          {
                              &lt;li class="@className"&gt;@Html.ActionLink(categoryInfo.Title, "Search", "Home", new {city = "all", type = "auction", category = categoryInfo.Name, keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                          }
                          else
                          {
                              &lt;li class="@className"&gt;@Html.ActionLink(categoryInfo.Title, "Search", "Home", new {city = "all", type = "product", category = categoryInfo.Name, keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                          }
  
                          crumpsCount--;
                      }
                  }
  </code></pre>
  
  <p>I just shared this code to see if this is a bug or not and if it is a bug is it a known one or not.</p>
  
  <p>Thanks to everyone. :)</p>
  
  <p><strong>Update:</strong>
  That's so stupid, now the code does not work with foreach loop either!</p>
  
  <p><img src="https://i.stack.imgur.com/q0a9O.png" alt="enter image description here"></p>
  
  <p>There are no indexing inside the foreach loop but the exception mentions that the index is out of range!</p>
  
  <p>Here is the code:</p>
  
  <pre><code>foreach (CategoryInfo categoryInfo in Model.Categories)
                      {
                          className = "bc" + crumpsCount;
  
                          if (Model.IsAuction)
                          {
                              &lt;li class="@className"&gt;@Html.ActionLink(categoryInfo.Title, "Search", "Home", new {city = "all", type = "auction", category = categoryInfo.Name, keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                          }
                          else
                          {
                              &lt;li class="@className"&gt;@Html.ActionLink(categoryInfo.Title, "Search", "Home", new {city = "all", type = "product", category = categoryInfo.Name, keyword = "all", condition = "all", sort = "asc"})&lt;/li&gt;
                          }
  
                          crumpsCount--;
                      }
  </code></pre>
  
  <p>I should also mention that Model.Categories is a IList&lt;> object.</p>