title: Declare variable in Razor
body: <p>I want to add a variable outside the foreach and then use that inside the foreach loop</p>
  
  <pre><code>&lt;table class="generalTbl"&gt;
      &lt;tr&gt;
          &lt;th&gt;Date&lt;/th&gt;
          &lt;th&gt;Location&lt;/th&gt;
      &lt;/tr&gt;
      @int i;
      @foreach (var item in Model)
      {
        i=0;
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.DueDate)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.location)
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  &lt;/table&gt;
  </code></pre>
  
  <p>The above example I have added @int i; outside of the foreach and I tried to access it inside the foreach like i=0; But it shows "<strong>The name 'i' does not exist in the current context</strong>"</p>
  
  <p>How can I access the variable inside the loop?</p>