Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Redirect to current page after form submission


Maybe this is more of a feature request instead of a question, but... Is there a way we can redirect to the current page that the form is living on after the form in submitted?

I know if you don't specify the "Display page after submission", this is already done, but the functionality is client-side and doesn't actually trigger a page reload.

My use case for this is all revolving around personalization with visitor groups. Say I want to hide the form and show some other block when the form is submitted. I could do this after the form is submitted, but I need to manually refresh the page for it to happen. It would be nice if we can just redirect to the current page.

I've explored how this could be done, and the main Response.Redirect occurs on the FormContainerBlockController, which is easily extendable. What I can't figure out is how to get the actual current page, as the request context is within the form's controller. I've also looked into changing the redirectUrl value in the DataSubmissionService. So far, I'm coming up blank with a clean way to do this.

Feb 23, 2021 5:13

Use the IPageRouteHelper service and the Page property to get to the current page regardless of where your code is based. I suppose you can inject the service inside the constructor of the FormContainerBlockController ? If it's not allowed I would suggest to use the Injected<Service> property or ServiceLocator.Current.

Feb 26, 2021 15:44

Thanks Giuliano. That was one of the original methods I tried, but had no success. I gave it another look and it's still not working. Here's some notes on what I'm observing:

  • In an extended FormContainerBlockController (created as seen in this documentation), if I clone and update the "RedirectToPage" property to the current page as provided in IPageRouteHelper, the submission doesn't trigger a page reload. I'm guessing because this is default behavior: if the "Display page after submission" property is empty, the "RedirectToPage" value is the current page, only the client-side submission messages are shown, and no redirect is needed.
  • I also looked into changing the "RedirectUrl" value as part of result in the "BuildReturnResultForSubmitAction" as part of the "DataSubmissionService". The value of the Page property in IPageRouteHelper is always the home page, so that doesn't work.
    • I can get this somewhat to work by using the HttpContext instead, and relying on the "HttpContext.Request.UrlReferrer.AbsolutePath", but that feels like a really bad hack.
Feb 26, 2021 18:40
- Feb 26, 2021 18:43
Ok, next attempt .. there is a way to retrieve the page from a block, you can retrieve the "page container" of the FormContainerBlock - I don't remember the code on top of my head but there's a way to retrieve it. From there you have the page that you need and you can redirect safely :)
Chris Sharp - Feb 26, 2021 18:46
I think this would still get back to that first point I noted. The IPageRouteHelper is correctly showing the current page, but the server-side reload of the page is not happening.
Chris Sharp - Feb 26, 2021 18:54
Actually, looking at the decompiled code again (it's been a while)... The "RedirectUrl" from the result provided in the "DataSubmissionService" is what's truly controlling the Response.Redirect. So getting that set, I think, is the key to figuring it out.

I re-read your comment, and I think I understand a bit more of what you are saying. The method I'm using in the DataSubmissionService gives me the FormContainerBlock object. Are you saying use that object to figure out the page? I don't think that would work since the block could be used on multiple pages.
- Feb 26, 2021 18:57
Hi Chris, you are absolutely correct about the fact that a block can be used in multiple page. However, the block that you can see as parameter only has one "page container" when this controller is being called - we need to find the right function to retrieve that container. I think it was something like an extension method called .GetOwner or something like that ? I am not fully sure.
Chris Sharp - Feb 26, 2021 19:50
I'm not seeing a way to get this information within the context of the DataSubmissionService, but honestly it feels like the right way to do this would be within the FormContainerController, and hooking onto the functionality surrounding the POST request. I'm playing around with trying to change the Response.Redirect, but I'm having trouble getting it to come back into the Index method after submission. Instead it's just keeping everything on the client-side.

Hi Chris,

I'm curious to know if you were able to find a viable solution for your redirection.


May 20, 2021 19:27
Chris Sharp - May 20, 2021 19:32
Nope. No solution yet. :(

I know this is a little late, but in case anyone else comes up with this issue, I worked around it with a multi-part solution. We had a client that wanted to create Gated Content using Epi/Opti forms and Visitor Groups. I was able to create this with a custom form block, OnSubmittedEvent, and then a little clean up with an AppendExtraInfoToRedirection override.

My article for Creating Gated Content with Optimizely Forms walks through my solution and links to the different parts I implemented.

Oct 08, 2021 14:18
Chris Sharp - Oct 08, 2021 15:57
Ahhhh yeaaah. Daved! Well done!
Though, IMHO, this functionality really needs to be part of the platform.
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.