Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Johan 50 posts 174 karma points
    Aug 03, 2014 @ 23:56
    Johan
    1

    MVC forms with controller without page refresh but with ajax instead?

    I've got this contact form working and I am happy with it:

    http://umbraco.com/follow-us/blog-archive/2013/7/14/moving-from-webforms-to-mvc.aspx

     

    but I am looking to do one adjustment. I want the form when it is posted not to do a postback but instead do a ajaxposting inside the Html page. Any advice of a equally excellent tutorial as the link above?

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Aug 05, 2014 @ 17:30
    Andy Butland
    0

    Not aware of an explicit tutorial, but the basic steps you would need would be:

    • Implement some JavaScript that hijacks the form post, and prevents it from posting normally.  
    • Collect the details of the form into a data structure, and use something like jquery's post method to do an AJAX post of that data to a surface controller method that has a matching parater in it's signature - the URL for this will be of the form /umbraco/surface/MyController/MyMethod
    • Have that method do what it needs to do server side and return a JsonResult with some form of response in
    • In the callback from the AJAX post you can check that JSON, and present the appropriate success or failure message to the user.
    Hope that helps you get started.
    Andy
Please Sign in or register to post replies

Write your reply to:

Draft