Copied to clipboard

Flag this post as spam?

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


  • Paul Griffiths 370 posts 1021 karma points
    Nov 16, 2015 @ 08:48
    Paul Griffiths
    0

    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; }
    
    }
    

    Strongly Typed (ContactFormViewModel) partial view

    @model uPortfolio.Models.ContactFormViewModel    
    @if (TempData["success"] == null)
    {
        // form post to the contact form surface controller submit action method
        using (Html.BeginUmbracoForm<uPortfolio.Controllers.ContactFormSurfaceController>("Submit"))
        {
            <div class="col-md-12">
                <div id="success"></div>
            </div>
        <fieldset>
            <legend>Contact Form</legend>
            <div class="col-md-6">
                <div class="form-group">
                    @Html.LabelFor(m => m.FullName)
                    @Html.TextBoxFor(m => m.FullName, new { placeholder = "Enter Full Name", @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.FullName)
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    @Html.LabelFor(m => m.Email)
                    @Html.TextBoxFor(m => m.Email, new { placeholder = "Enter Email", @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Email)
                </div>
            </div>
            <div class="col-md-12">
                <div class="form-group">
                    @Html.LabelFor(m => m.Message)
                    @Html.TextAreaFor(m => m.Message, new { placeholder = "Enter Message", @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Message)
                </div>
                <input type="submit" id="submit" name="submit" value="Send Message" class="btn btn-primary btn-lg btn-block btn-biru" />
            </div>
        </fieldset>
    
        }
    }
    else
    {
    // confiirmation
       Thanks for your request
    }
    

    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.

    Many thanks Paul

  • Jordan Lane 28 posts 130 karma points c-trib
    Nov 16, 2015 @ 17:34
    Jordan Lane
    1

    Hi Paul,

    You could return a redirect to the URL if you think that wouldn't change e.g.

    return Response.Redirect('/thank-you');
    

    The other option would be to find your thank you page in Umbraco using the DocTypeAlias or something similar.

  • Paul Griffiths 370 posts 1021 karma points
    Nov 17, 2015 @ 08:12
    Paul Griffiths
    0

    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

  • Jack 3 posts 73 karma points
    Mar 04, 2016 @ 21:34
    Jack
    0

    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?

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Mar 05, 2016 @ 11:25
    Dennis Adolfi
    1

    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);
    
Please Sign in or register to post replies

Write your reply to:

Draft