title: Incrementing a Razor Variable in JavaScript isn't Working
body: <p>I tried doing an allocation page in which if you wish to add extra person, you must click the "add one more" button. I had a count variable and first time when I clicked the button and called the function, things worked well, but when I click the button second time I realized that the previous increment of count isn't getting reflected. How should I make the increment permanent or any other solution using which I can achieve this.  </p>
  
  <pre><code>@{
      int count = 1;
  }
  &lt;script&gt;
  function AddNewDoc(ElementId) {
  //This Increment is not reflected when the function is called again
  @{
  count++;
  }
  
  document.getElementById(ElementId).innerHTML = "&lt;td&gt;A&lt;/td&gt;" + "&lt;td&gt;B&lt;/td&gt;" + "    &lt;td&gt;C&lt;/td&gt;" + "&lt;td&gt;&lt;input type=\"button\" value=\"Add One More Doctor\"      onclick=\"AddNewDoc(@count)\" /&gt;&lt;/td&gt;&lt;/tr&gt;" + "&lt;tr id=\"@count\"&gt;";
   }
  &lt;/script&gt;
  @Html.ActionLink("GO BACK TO OPD POSTING", "UnitAndMonth", "OPD")
      &lt;br /&gt;
  
      &lt;br /&gt;
      &lt;h2&gt;@ViewBag.msg&lt;/h2&gt;
  
      @using (Html.BeginForm("HandleForm", "OPD", FormMethod.Post))
      {  &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;
              @Html.TextBoxFor(x =&gt; x[count].dr_name, new { @class = "input1", @id = "tags" })
          &lt;/td&gt;
          &lt;td&gt;
              @Html.TextBoxFor(x =&gt; x[count].from_date, new { @class = "input1", @id = "datepicker" + count, @readonly = "readonly" })
          &lt;/td&gt;
  
          &lt;td&gt;
              @Html.TextBoxFor(x =&gt; x[count].to_date, new { @class = "input1", @id = "datepicker" + count + count, @readonly = "readonly" })
          &lt;/td&gt;
          &lt;td&gt;
              &lt;input type="button" value="Add One More Doctor" onclick="AddNewDoc(@count)" /&gt;
          &lt;/td&gt;
  
      &lt;/tr&gt;
        &lt;tr id="@count"&gt;&lt;/tr&gt;
  
      &lt;/table&gt;
          &lt;input type="submit" value="Submit" /&gt;
      }
  </code></pre>