title: ASP.NET MVC Post Controller Action Invoked multiple times
body: <p>I have an MVC application which is a very simple form of 6 fields. The form data is saved to my database via Entity Framework. Most of the time the data saves properly without issue, but randomly duplicates are appearing. I first thought it was a EF issue with duplicates (sometimes 1,3,5 of the same record) being saved with a timestamp of less than a second separating them. But I ran a unit test and pulled out all of the same logic that is in my controller action to save the data and my data never gets duplicated. I have JQuery that disables the submit button so I know its not related to multi-clicks.</p>
  
  <p>So my question: is it possible that my controllers POST action can get called numerous times through some unknown behavior? I cannot replicate the issue in VS when i set a breakpoint in my controller action where the data is being saved. I also find it difficult to replicate in general and is completely random. Has anyone come across this ever or have any suggestions? It is occuring in all browsers (FF, IE, Chrome). Thanks</p>
  
  <p>I ran my application through fiddler and on occasion when i submit my form I do see 2 POST requests on 1 single button click.</p>
  
  <p>Code</p>
  
  <pre><code>@model PersonViewModel
  @using (Html.BeginForm())
      {
      @Html.AntiForgeryToken()
  
  &lt;div class="form-horizontal"&gt;
  
  
      @Html.ValidationSummary(false, "The following errors have occurred:", new { @class = "alert alert-danger", @role = "alert" })
  
  
  
      &lt;hr /&gt;
  
          &lt;p&gt;Please enter your personal details below. (* denotes required field)&lt;/p&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.FirstName, new { htmlAttributes = new { @class = "form-control", @maxlength = "50", @title = "Legal First Name" } })
                  @Html.ValidationMessageFor(model =&gt; model.FirstName, "", new { @class = "text-danger" })
  
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.MiddleName, new { htmlAttributes = new { @class = "form-control", @maxlength = "50", @placeholder = "enter 'None' if none", @title = "Legal Middle Name" } })
                  @Html.ValidationMessageFor(model =&gt; model.MiddleName, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.LastName, new { htmlAttributes = new { @class = "form-control", @maxlength = "50", @title = "Legal Last Name" } })
                  @Html.ValidationMessageFor(model =&gt; model.LastName, "", new { @class = "text-danger" })
  
  
              &lt;/div&gt;
          &lt;/div&gt;
  
  
  
          &lt;div class="form-group" id="EmailGroup"&gt;
              @Html.LabelFor(model =&gt; model.EmailAddress, htmlAttributes: new { @class = "control-label col-md-2", @id = "EmailLabel" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.EmailAddress, new { htmlAttributes = new { @class = "form-control", @maxlength = "100", @title = "Email Address" } })
                  @Html.ValidationMessageFor(model =&gt; model.EmailAddress, "", new { @class = "text-danger" })
  
  
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;/div&gt;
  
  
  
  
  
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-2"&gt;&lt;/div&gt;
              &lt;div class="col-md-8"&gt;
                  &lt;input type="submit" value="Submit" id="affSubmit" class="btn btn-primary btn-md" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
      &lt;/div&gt;
  
  
                      }
  
  
  @section Scripts {
  @Scripts.Render("~/bundles/jqueryval")
  
  @Scripts.Render("~/Scripts/Custom.js")
  
  
  
  }
  </code></pre>
  
  <p>Layout.cshtml</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8" /&gt;
  &lt;meta http-equiv="X-UA-Compatible" content="IE=edge" /&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  @Styles.Render("~/Content/css")
  @Scripts.Render("~/bundles/modernizr")
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class="navbar navbar-inverse navbar-default"&gt; 
  
  &lt;/div&gt;
  &lt;div class="container body-content"&gt;
      @RenderBody()
      &lt;hr /&gt;
  &lt;/div&gt;
  @Scripts.Render("~/bundles/jquery")
  @Scripts.Render("~/bundles/bootstrap")
  @Scripts.Render("~/bundles/jquery-ui")
  @RenderSection("scripts", required: false)
  </code></pre>
  
  <p>
      </p>
  
  <p>Custom.js</p>
  
  <pre><code>$().ready(function () {
  
  
  
  
  $('form').submit(function (e) {
  
      if ($('form').valid()) { //make sure form is valid
  
                  $('#affSubmit').prop('disabled', true);
                  $('#affSubmit').val("Please wait...");
                  this.submit();
  
  
          }
  
  
      }
  
  
  
  
  });
  </code></pre>
  
  <p>ControllerAction</p>
  
  <pre><code>[HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Index(PersonModel model)
      {
  
          try
          {
  
                  if (ModelState.IsValid)
                  {
  
                      PersonBLL bll = new PersonBLL();
                      PersonModel result = bll.SavePerson(model);
  
                      if (result.PersonID &gt; 0)
                      {
                          return View("CreateSuccess");
                      }
                      else
                      {
                          ModelState.AddModelError("SaveError", "An error occurred while attempting to save, please try again later.");
                      }
  
                  }
  
          }
          catch (Exception ex)  //unexpected error so log it, and redisplay form
          {
              Common.HandleException(ex);
              ModelState.AddModelError("SaveError", "An error occurred while attempting to save, please try again later.");
  
          }
  
  
          //if we get here reshow page
          return View(model);
  
  
      }
  </code></pre>