locked
How do I run Controller methods from a View webpage? RRS feed

  • Question

  • User-404502133 posted

    Hi,

    VS2013, MVC5, VB

    I want to execute some server side code after the user clicks on a button and before the Form Submit takes place.  The following is what I have in place that is working (from a post on this forum):

    ...stuff
    
    <form id="simForm" method='post' target="_blank" action='https://test.authorize.net/gateway/transact.dll'>
    
      ...other stuff...
      <input type='button' onclick="UserCommit()" value="Submit Payment" class="btn btn-warning" />
    
    </form>
    
    <script type="text/javascript">
      function UserCommit() {
        $("#simForm").submit()
      }
    </script>
    

    This works with or without the type="text/javascript" attribute; I'm not certain I understand why.  At any rate, when the button is clicked the function 'UserCommit' is executed and the script executes the Submit.  So that's all pretty cool.  I then tried to insert the following 'get' call, but it doesn't seem to be working:

    <script type="text/javascript">
      function UserCommit() {
        $.get("@Url.Action("ActionsBeforeSubmit", "Products")")
        $("#simForm").submit()
      }
    </script>

    So I have a few questions about how this will work:

    Q1) It would seem the controller action I'm calling can't be an ActionResult but needs to be a regular sub/function because doesn't an ActionResult drive another web page?

    Q2) Am I supposed to be using a different syntax other than the <script> tags?

    If anyone can help with the proper method to use to execute code back on the server before the script executes the Submit that would be great.

    Thanks.

    Best Regards,

    Alan

    Tuesday, December 9, 2014 5:12 PM

Answers

  • User-760709272 posted

    Rather than using get, use $.ajax and set the asynch option to false

    http://api.jquery.com/jquery.ajax/

    that will cause your script to wait until the action call has finished before continuing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 11, 2014 3:45 AM

All replies

  • User-760709272 posted

    Make sure the action method isn't marked [HttpPost] as if it is you can't "get" it.  Also check the network section of your browser's dev tools to get a better idea what is happening

    http://forums.asp.net/t/1982579.aspx?Using+the+browser+s+dev+tools+to+diagnose+ajax+problems+and+other+things+

    You can still use something that returns ActionResult, if it doesn't do anything, use

    return new EmptyResult();

    Wednesday, December 10, 2014 3:53 AM
  • User-404502133 posted

    Hi Aidy,

    Thanks for your response.  I think everything was successful, but I had a few issues and questions.

    Success #1: I was able to engage the browser tool per your instructions at that link.  So that was cool.

    I had a little problem.  In the debugger it Initially came up saying "The script debugger failed to connect to the target process. A debugger is already attached."  I thought maybe I needed to start Visual Studio without its debugger.  After that the IE debugger seemed to work OK, but I don't know if that was for certain that problem.  The thing is, even when running in Visual Studio debug mode, some pages seem to debug in the IE debugger OK.  Any thoughts?

    Success #2: On the page where I run the script as posted above I was able to see the script, set breakpoints, and generally confirm that the script area is running.  Trying to step through the script seemed a little fruitless for me as a newbie on this stuff.

    Success #3: After creating a new blank controller and adding just a single ActionResult method, I was able to successfully run my Controller Action after clicking on the button we created.  I had set a breakpoint in Visual Studio to catch the execution of my ActionResult called from $.get(...) in the webpage script and it hit that breakpoint just fine, but the .submit in the script executed even though the ActionResult was not completed.

    I don't think I want a payment process to take place unless I know everything in the ActionResult completed successfully.

    Is there a way to force the script to wait for the $.get(...) to complete before proceeding?  Or is that in the nature of Ajax and not alterable?  It would seem I should be able to wait.

    Thanks.

    Best Regards,

    Alan

    Wednesday, December 10, 2014 9:57 PM
  • User-760709272 posted

    Rather than using get, use $.ajax and set the asynch option to false

    http://api.jquery.com/jquery.ajax/

    that will cause your script to wait until the action call has finished before continuing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 11, 2014 3:45 AM
  • User-404502133 posted

    Hi Aidy,

    Well, the successful end of another thread you've helped me with.  I thought I should post my final solution, and I note a few questions afterward.

    This is the Controller Code:

    Public Class MiscController
      Inherits Controller
      Public Function ActionsBeforeSubmit() As ActionResult
        ClearCart()
        Return New EmptyResult()
      End Function
    End Class

    This is the relevant View Code:

    <form id="simForm" method='post' target="_blank" action='https://test.authorize.net/gateway/transact.dll'>
    ...PAGE CONTENT HERE <input type='button' onclick="UserCommit()" value="Submit Payment" class="btn btn-warning" /> </form>
    <script > function UserCommit() { $.ajax({ url: '@Url.Action("ActionsBeforeSubmit", "Misc")' }) $("#simForm").submit() } </script>

    Pretty simple.  Thanks for the help.  Exactly what I wanted for this particular thread.  Interestingly, the ajax above waits for ActionsBeforeSubmit to complete before submitting.  I didn't have to use any options; did I do something incorrect, or am I inviting trouble without specifying the option?

    BTW, do you have any particular ajax/javascript books you recommend for getting up to speed?  This is one area I've played with, but never really developed into.

    Thanks again.

    Best Regards,

    Alan

    Thursday, December 11, 2014 4:04 PM