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:
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:
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
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:
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:
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:
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
is working on a reply...