Is it possible to redirect to a different url - for example a Thank you page from a surface controller. Im using the following but the Thank you page is inside the Original page - I need it to redirect to a new URL.
I have used the following:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SubmitPersonalDetailsForm(PersonalDetails model)
{
return new RedirectResult(ViewDelegateURL, true);
return this.RedirectToUmbracoPage(ViewDelegateId);
Add an onsuccess function and assign it in your ajaxform options.
var onSuccess = function(result) {
if (result.url) {
// if the server returned a JSON object containing an url
// property we redirect the browser to that url
window.location.href = result.url;
}
For my Umbraco v13+ website, below is how I managed to do my redirects. In older versions of Umbraco, maybe "Redirect" or "RedirectToUmbracoPage" could work but not for v13+ websites as far as I see.
Razor view:
@inject IUmbracoContentHelperService _umbracoContentHelperService
@inject IGoogleAnalyticsHelperService _googleAnalyticsHelperService
@inherits UmbracoViewPage<ResetPasswordFormViewModel>
@model ResetPasswordFormViewModel
@{
var portalPageUrl = _umbracoContentHelperService.GetPortalEditAccountPage()?.Url() ?? "/";
var passwordUpdateSuccessful = TempData["ResetPasswordIsSuccessful"] as bool? == true;
var displayCurrentPasswordIncorrectMessage = passwordUpdateSuccessful == false && TempData["DisplayCurrentPasswordIncorrectMessage"] as bool? == true;
}
<div>
@if (passwordUpdateSuccessful)
{
<p>Your password has been successfully changed.</p>
<p><a href="@portalPageUrl" class="btn btn-pink">Back</a></p>
<script>
try {
window.dataLayer.push(@(Html.Raw(_googleAnalyticsHelperService.GetPortalAnalytics("reset password"))))
} catch (e) {
console.log("Failed to push data to GA.");
}
</script>
}
else if (displayCurrentPasswordIncorrectMessage)
{
<p>Your current password is not correct.</p>
<p><a href="@portalPageUrl" class="btn btn-pink">Back</a></p>
}
else
{
using (Html.BeginUmbracoForm<ResetPasswordController>("Reset", FormMethod.Post))
{
@*Starting from Umbraco 9 the __RequestVerificationToken token is automatically added to forms for use, so we no longer need to add @Html.AntiForgeryToken() to our forms.*@
@*For details: https://docs.umbraco.com/umbraco-cms/reference/routing/surface-controllers#protecting-surface-controller-routes*@
@Html.HiddenFor(m => m.Token)
<div class="profile-preferences-form__field">
@Html.LabelFor(x => x.CurrentPassword)
<div class="w-100">
@Html.TextBoxFor(x => x.CurrentPassword, new { type = "password", maxlength = 64 })
@Html.ValidationMessageFor(x => x.CurrentPassword)
</div>
</div>
<div class="profile-preferences-form__field">
@Html.LabelFor(x => x.Password)
<div class="w-100">
@Html.TextBoxFor(x => x.Password, new { type = "password", maxlength = 64 })
@Html.ValidationMessageFor(x => x.Password)
</div>
</div>
<div class="profile-preferences-form__field">
@Html.LabelFor(x => x.ConfirmPassword)
<div class="w-100">
@Html.TextBoxFor(x => x.ConfirmPassword, new { type = "password", maxlength = 64 })
@Html.ValidationMessageFor(x => x.ConfirmPassword)
</div>
</div>
<p>@RegexConstants.PasswordRulesUserNotice</p>
<input type="submit" class="btn btn-pink" title="Submit" value="Submit" />
<a href="@portalPageUrl" class="btn btn-default ml-4">Back</a>
}
}
</div>
Here is the surface controller:
public class ResetPasswordController : SurfaceController
{
// Removed code for brevity
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<IActionResult> Reset(ResetPasswordFormViewModel model)
{
if (!ModelState.IsValid)
{
return CurrentUmbracoPage();
}
if (await _memberManager.ValidateCredentialsAsync(model.Email, model.CurrentPassword))
{
// Removed code for brevity
// Set TempData to true to display the confirmation message
TempData["ResetPasswordIsSuccessful"] = true;
}
else
{
// Removed code for brevity
// Current password is not correct
// Set TempData to true to display the error message
TempData["ResetPasswordIsSuccessful"] = false;
TempData["DisplayCurrentPasswordIncorrectMessage"] = true;
}
return RedirectToCurrentUmbracoPage();
}
}
Redirect from Surface Controller to different URL
Hi,
Is it possible to redirect to a different url - for example a Thank you page from a surface controller. Im using the following but the Thank you page is inside the Original page - I need it to redirect to a new URL.
I have used the following:
Any ideas what I need to do to get this working?
Hi Jon,
Can't you just use
return Redirect(ViewDelegateURL);
?-Joep
I must be doing something wrong - it doesnt actually redirect - but puts the new page inside the old page
you should definitely just be able to do
return Redirect("/some-url");
or
return RedirectToUmbracoPage(umbracoPageId);
Would this stop it from working?
My Front end partial form is using this:
@using (Ajax.BeginForm("SubmitPersonalDetailsForm", "PersonalDetailsSurface", new AjaxOptions() {
In your controller, return JSON
return Json(new { url = yoururl });
Add an onsuccess function and assign it in your ajaxform options.
}
Yes, you can't redirect from an Ajax post, you need to return the URL and redirect in the onsuccess function
For my Umbraco v13+ website, below is how I managed to do my redirects. In older versions of Umbraco, maybe "Redirect" or "RedirectToUmbracoPage" could work but not for v13+ websites as far as I see.
Razor view:
Here is the surface controller:
Related links: https://docs.umbraco.com/umbraco-cms/13.latest/reference/routing/surface-controllers/surface-controllers-actions
https://docs.umbraco.com/umbraco-cms/reference/routing/surface-controllers#protecting-surface-controller-routes
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.