title: Cancel MVC Form submit
body: <p>I am submitting a form using AJAX. The fields are in a partial view that get sent to a controller action. The partial view is in a div that pops up when a button on the main form is clicked. If the user completes the form and clicks the submit button it then the update is performed in the controller action and all is well.  However, if the user changes their mind I like to add a cancel button to hide the form.  Adding the button and hiding the form works as expected, but the call to the controller action method still occurs.  I've tried using</p>
  
  <pre><code>e.preventDefault();
  </code></pre>
  
  <p>but this hasn't worked.</p>
  
  <p>I've tried using a second JQuery method attached to the cancel id but I can't get this to work.</p>
  
  <p>My JQuery looks like this:</p>
  
  <pre><code>$('#VisitorModal').on('submit', '#visitorform', function (e) {
      var data = $(this).serialize();
      var url = '@Url.Action("CreateVisitor", "Meetings")';
      var text = $('#title').val() + ' ' + $('#firstname').val() + ' ' + $('#surname').val() + ' (' + $('#company').val() + ')'; // a value from the form that you want as the option text
      $.post(url, data, function (response) {
          if (response) {
              $('#VisitorID').append($('&lt;option&gt;&lt;/option&gt;').val(response).text(text)).val(response);
          } else {
              dialog.hide();
          }
      }).fail(function () {
          dialog.hide();
      });
      dialog.hide();
      e.preventDefault();
      //return false; // cancel the default submit
  });
  
  $('#cancel').on('click', function () {
      dialog.hide();
  });
  </code></pre>
  
  <p>And here's my action method:</p>
  
  <pre><code>[HttpPost]
  public JsonResult CreateVisitor(AppointmentViewModel model)
  {
      var visitor = new Visitor()
      {
          Title = model.VisitorTitle,
          FirstName = model.VisitorFirstName,
          LastName = model.VisitorSurname,
          Company = model.VisitorCompany
      };
      db.Visitors.Add(visitor);
      db.SaveChanges();
      return Json(visitor.id);
  }
  </code></pre>
  
  <p>And submit &amp; cancel buttons here:</p>
  
  <pre><code>&lt;div class="form-group"&gt;
      &lt;div class="col-md-offset-2 col-md-5"&gt;
          &lt;input type="submit" value="Create" class="btn btn-success" id="submit" /&gt;
          &lt;input type="button" value="Cancel" class="btn btn-warning" id="cancel" /&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Does anyone have any suggestions that might get this to work as I hope?</p>