First Last

First Last

  • 1.5k
  • 528
  • 7.4k

How to fire off an ActionLink (controller method) defined in a view.

Oct 10 2020 9:02 AM
I have an ASP.NET MVC app with a view that has Form, some buttons and modal popups.
 
My code for the save button works fine. I am not including that code below.
 
My problem is the cancel button.
 
When that button is clicked, I prevent the default behavior from happening so I can present a modal.
 
From the 'yes' selection of the cancel button modal, I now want to fire off that default behavior which is the controller action method defined in the actionlink for that button. When clicked, the method gets the data again and returns the entire same view in it's original state before changes were made - hence cancelling the changes.
 
I don't think I can use an AJAX call in the yes modal to call that action method as that method re retrieves the data and sends back the entire view.
 
In the "success:" of the AJAX call, I would have to remove the entire current view and then replace it with the view sent back. But how can I remove the current view as I would be removing the jQuery code I am executing as it is part of the view?
 
So how can I call that action method that returns the entire view and re-render the view returned?
 
The view (simplified):
  1. @model GbngWebClient.Models.UserProfileForMaintVM    
  2.     
  3. @using (Html.BeginForm("ProcessSaveUserProfile""UserProfile", FormMethod.Post, new { enctype =     
  4. "multipart/form-data", id = "formId" }))    
  5. {    
  6.     <div class="panel-body">    
  7.         <div class="row">    
  8.             <div class="form-group">    
  9.                 <div class="col-md-3">    
  10.                     <input type="submit" value="Save" class="btn btn-primary saveButton" />    
  11.                 </div>    
  12.                 <div class="col-md-3">    
  13.                     @Html.ActionLink("Cancel Changes""GetUserProfile""UserProfile"nullnew {     
  14. @class = "btn btn-info cancelButton" })    
  15.                 </div>    
  16.             </div>    
  17.         </div>    
  18.     </div>    
  19. }    
  20.     
  21. <div class="modal fade" id="myModal13" role="dialog" display="none">    
  22.     <div class="modal-dialog">    
  23.         <div class="modal-content">    
  24.             <div class="modal-body" style="padding:10px;">    
  25.                 <h4 class="text-center">Any unsaved changes will not be processed if you cancel.       
  26. Continue ?</h4>    
  27.                 <div class="text-center">    
  28.                     <a class="btn btn-info btn-yes13">Yes</a>    
  29.                     <a class="btn btn-default btn-no13">No</a>    
  30.                 </div>    
  31.             </div>    
  32.         </div>    
  33.     </div>    
  34. </div>    
  35.     
  36. @Scripts.Render("~/bundles/jqueryval")    
  37. @Scripts.Render("~/bundles/jquery")    
  38. @Scripts.Render("~/bundles/bootstrap")    
  39. @Styles.Render("~/Content/css")    
  40.     
  41. <script type="text/javascript">    
  42.     $(document).ready(function () {    
  43.         $(".cancelButton").click(function (e) {    
  44.             // Trigger(show) the modal.    
  45.             $("#myModal13").modal({    
  46.                 backdrop: 'static',    
  47.                 keyboard: false    
  48.             });    
  49.     
  50.             // Prevent the ActionLink from going to the controller.    
  51.             e.preventDefault();    
  52.         });    
  53.                
  54.         $('.btn-yes13').click(function() {    
  55.             // Hide the modal.    
  56.             $('#myModal13').modal('hide');    
  57.     
  58.             // CONCERN: HOW TO NOW FIRE THE ActionLink TO GO TO THE CONTROLLER METHOD?    
  59.     
  60.             // Return.    
  61.             return true;    
  62.         });    
  63.     
  64.         $(".btn-no13").click(function () {    
  65.             $("#myModal13").modal("hide");    
  66.     
  67.             return false;    
  68.         });    
  69.     
  70.         // Remove the modal once it is closed.    
  71.         $("#myModal13").on('hidden.bs.modal'function () {    
  72.             $("#myModal13").remove();    
  73.         });     
  74.     });       
  75. </script>  

Answers (5)