Redirecting to umbraco thank you page template from surface controller
Hi all,
I am creating a little portfolio site to try and learn umbraco 7, Razor & MVC and I have run into a little issue. The version I am working with is 7.3.0. Its a single layout website where I have used partial views to populate each section of the homepage e.g. CarouselSection, AboutSection, ContactSection etc.
I have followed the surfaceController video on Umbraco TV (http://umbraco.tv/videos/developer/fundamentals/surface-controllers/introduction/) and I have the following set up
Surface Controller
[HttpPost]
public ActionResult Submit(ContactFormViewModel model)
{
//check that all mandatory fields have values and they are well formed
//if not lets return the current page and display validation errors
if (!ModelState.IsValid)
{
return CurrentUmbracoPage();
}
//send email but save in a PickupDirectoryLocation for now (set in web config)
var message = new MailMessage();
message.To.Add("[email protected]");
message.Subject = "New Contact request";
message.From = new MailAddress(model.Email, model.FullName);
message.Body = model.Message;
var smtp = new SmtpClient();
smtp.Send(message);
TempData["success"] = true;
return RedirectToCurrentUmbracoPage();
}
Model
public class ContactFormViewModel
{
[Required]
[Display(Name = "Full Name")]
public string FullName { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Message { get; set; }
}
I then have a partial view macro to make a call to the controllers GET request to render the form
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@*make a call to the render contact form action method (GET) on the contact form surface controller*@
@Html.Action("RenderContactForm", "ContactFormSurface")
which is called in another partial view called contactSection.cshtml
<div class="col-md-8 col-sm-8">
@*call the umbraco macro which calls the contactFormMacro Partial*@
@Umbraco.RenderMacro("ContactFormMacro")
</div>
This all appears to work fine but what i dont like is that when the email is sent succesfully and we redirect to the currentUmbracoPage i am unable to maintain the postback postback position and teh page returns to the top meaning the user has to scroll right down to the bottom of the page (single page layout) to see the 'Thanks for your request' message.
With this in mind I thought a better approach may be to redirect the user to a thank you page instead. I was thinking of creating a new contactThankYou docType & corrsponding template and redirecting the users to that upon success.
What is the best way to achieve this please? I see that there is a RedirectToUmbracoPage method which takes a pageId but i dont really want to rely on pageIds incase its ever deleted from the back office.
//return RedirectToUmbracoPage(1090);
Could really do with some advice on the best approach if someone could please help.
Hi Paul. (And Jack, seeing that Paul´s post is a bit old)
The nicest way in my opinion when using a form on a single-page layout is to send the form as an ajax request, and from that ajax request return a partial view that replaces the form containers html. That way, you dont get the annoying postback and everything feels alot cleaner.
If you are not comfortable with ajax i could send you some code example.
But, if you´d like to redirect the user to a different page, but you dont want to "hard-code" the pageId value, you could always have a "Content-picker" property on your page, named "Thank you page" and in the backoffice, the user has to select from the content tree which page should be the "Thank you" page.
So instead of your current ActionResult returning:
return RedirectToUmbracoPage(1090);
You return the selected thankyou page for the current page:
int thankYouPageId = Int32.Parse(CurrentPage.GetProperty("thankYouPage").Value);
return RedirectToUmbracoPage(thankYouPageId);
Redirecting to umbraco thank you page template from surface controller
Hi all,
I am creating a little portfolio site to try and learn umbraco 7, Razor & MVC and I have run into a little issue. The version I am working with is 7.3.0. Its a single layout website where I have used partial views to populate each section of the homepage e.g. CarouselSection, AboutSection, ContactSection etc.
I have followed the surfaceController video on Umbraco TV (http://umbraco.tv/videos/developer/fundamentals/surface-controllers/introduction/) and I have the following set up
Surface Controller
Model
Strongly Typed (ContactFormViewModel) partial view
I then have a partial view macro to make a call to the controllers GET request to render the form
which is called in another partial view called contactSection.cshtml
This all appears to work fine but what i dont like is that when the email is sent succesfully and we redirect to the currentUmbracoPage i am unable to maintain the postback postback position and teh page returns to the top meaning the user has to scroll right down to the bottom of the page (single page layout) to see the 'Thanks for your request' message.
With this in mind I thought a better approach may be to redirect the user to a thank you page instead. I was thinking of creating a new contactThankYou docType & corrsponding template and redirecting the users to that upon success.
What is the best way to achieve this please? I see that there is a RedirectToUmbracoPage method which takes a pageId but i dont really want to rely on pageIds incase its ever deleted from the back office.
Could really do with some advice on the best approach if someone could please help.
Many thanks Paul
Hi Paul,
You could return a redirect to the URL if you think that wouldn't change e.g.
The other option would be to find your thank you page in Umbraco using the DocTypeAlias or something similar.
Hi Jordan,
Thanks for providing a response. I will take a look into what you suggested, i think i will probably try and implement the DocTypeAlias way.
I will provide an update when i get this working :)
Cheers
Paul
Hi Paul
Did you ever find a way to redirect the current page to the post position?
Or did you just end up using a separate thank you page?
Hi Paul. (And Jack, seeing that Paul´s post is a bit old)
The nicest way in my opinion when using a form on a single-page layout is to send the form as an ajax request, and from that ajax request return a partial view that replaces the form containers html. That way, you dont get the annoying postback and everything feels alot cleaner. If you are not comfortable with ajax i could send you some code example.
But, if you´d like to redirect the user to a different page, but you dont want to "hard-code" the pageId value, you could always have a "Content-picker" property on your page, named "Thank you page" and in the backoffice, the user has to select from the content tree which page should be the "Thank you" page.
So instead of your current ActionResult returning:
You return the selected thankyou page for the current page:
is working on a reply...