Mad Man

Mad Man

  • NA
  • 2
  • 827

Code Not Working when Switch To AJAX From Formaction

Jun 24 2018 5:06 AM
I am trying to call an MVC controller method to delete a selected jqGrid row using AJAX instead of using the form's formaction attribute on a view but thus far it is not working.
 
When I use formaction to call my controller method everything worked fine but it did not implement a confirm modal which asks users to confirm if they really want to delete the selected grid row.
 
Thus, I want to use a confirm modal in conjuction with AJAX to call my controller method. Below is the form code in my MVC view.
  1. @using (Html.BeginForm("Delete""CustomerForms", FormMethod.Post))  
  2. {  
  3. <div>.....</div>  
  4. <div>.....</div>  
  5. <button class="btn" id="Delete" type="submit"formaction="DeleteRecord"formmethod="post" name="command"value="Delete">Delete </button>  
  6. }  
The controller method DeleteRecord takes no input parameter so I thought I can just set the formaction =" #" and then call the controller method using AJAX like the following inside
  1. $("#Delete").click(function (e) {  
  2.    e.preventDefault();  
  3.    $("#DeleteDialog").dialog("open");  
  4. });  
  5.    
  6. $("#DeleteDialog").dialog({  
  7.    height: 280,  
  8.    modal: true,  
  9.    autoOpen: false,  
  10.    buttons: {  
  11.      'Confirm'function () {  
  12.        $.ajax({  
  13.          type: "POST",  
  14.          cache: false,  
  15.          url: "/CustomerForms/DeleteRecord",  
  16.          success: function (data) {  
  17.            alert("Deleted!");  
  18.          }  
  19.        });  
  20.        $(this).dialog('close');  
  21.    },  
  22.      'Cancel'function () {  
  23.         $(this).dialog('close');  
  24.      }  
  25.    }  
  26. });  
  27. @{var message = ViewBag.Message;}  
  28. @if (message == "Deleted")  
  29. {  
  30.   ViewBag.Message = null;  
  31.   jQuery("#jQGrid").clearGridData(true).trigger("reloadGrid");  
  32.   jQuery('#jqGrid').trigger( 'reloadGrid' );  
  33.   $('#files').empty();  
  34. }  
Normally if everything had worked successfully, the script below the delete dialog would run, the viewBag value would be set to null, and the jqGrid would reload resulting in the deletion of the selected grid row.
 
However, when I click the Delete button, the controller is executed like it's supposed to but the script below the delete dialog is skipped and doesn't get executed.

Answers (1)