Copied to clipboard

Flag this post as spam?

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


  • Tom 713 posts 954 karma points
    Mar 07, 2014 @ 01:17
    Tom
    0

    Current Page doesn't exist in SurfaceController With Ajax Form

    I'm getting an exception: Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request

     

    When trying to use CurrentPage in a surfacecontroller with a form posted with ajax.

    Just wondering if there's a workaround?

    Cheers

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Mar 08, 2014 @ 22:51
    Andy Butland
    0

    As you've found if a request is posted via AJAX you can't use CurrentPage to find out what the current page is.  There's a couple of workarounds I've used.

    One is to find the page using based on the document type - e.g. if I know the request is only ever going to be coming from a page that is the only instance of a document type, I can do something like this:

    var pageNode = Umbraco.TypedContentAtRoot().First()
        .Descendants("MyUnqiueDocType")
        .FirstOrDefault();

    If that's not possible though, then you need to pass in the Id of the current page with the request. One way to do this would be to add the Id of the page somewhere in the DOM (e.g. in the value of a hidden input, text of a hidden span, or a data- attribute) and then pull that out before making the AJAX request.

    In the controller action you can then do something like:

    var pageNode = Umbraco.TypedContent(pageId);
    
  • Tom 713 posts 954 karma points
    Mar 09, 2014 @ 23:30
    Tom
    0

    Thanks for the reply Andy :)

     

    What I ended up doing was use BeginUmbracoForm but append the appropriate data attributes that a usual ajax.beginform would append to get the right ajax behaviour. I then got the surface controller to send back a JsonResponse:

     

     @using (Html.BeginUmbracoForm<SignUpController>("Register", FormMethod.Post, new { @id = "sign-up-form", @class = "form quick-contact", data_ajax = "true", data_ajax_failure = "SignUpForm.showError(data)", data_ajax_loading = "#contact-load", data_ajax_method = "POST", data_ajax_complete = "SignUpForm.complete" }))
  • Charles Afford 1163 posts 1709 karma points
    Mar 10, 2014 @ 22:27
    Charles Afford
    0

    Hi Tom what do you need to do with it?

    This happens when you do AJAX out of a partial view.  Its seems to work in a View.

    You can use the Umbraco methods ReturntoUmbracoPage(int id) i think or CurrentUmbracoPage()

    Also i have found if you do string url = ReturntoUmbracoPage(int id).Url  and then ResponseRedirect(url);

    this keeps the context as well, so you could append a query string or pass a model ect.  Hope this helps :).  Charlie

Please Sign in or register to post replies

Write your reply to:

Draft