Copied to clipboard

Flag this post as spam?

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


  • Istvan Pszota 59 posts 191 karma points
    Mar 22, 2020 @ 17:31
    Istvan Pszota
    0

    Surface Controller form post redirects to /Umbraco/Surface

    Hi Guys!

    I'm trying to put together a form using Surface controller, but when submitting the form, the browser redirects to /umbraco/Surface/InsightsSignup/SubmitForm

    The values from the form arrives to the controller, strangely.

    I'm using Umbraco v8.4.0

    View:

    @inherits UmbracoViewPage<InsightsSignupModel>
    
    @using package.Models
    @using package.Controllers
    
    
            @if (TempData["value"] != null)
            {
                <p>Done</p>
            }
            else
            {
                using (Html.BeginForm("SubmitForm", "InsightsSignup", FormMethod.Post))
                {
                    @Html.TextBoxFor(m => m.Name, new { @class = "form-control", placeholder = "Name..." })
                    @Html.TextBoxFor(m => m.Company, new { @class = "form-control", placeholder = "Company..." })
                    @Html.TextBoxFor(m => m.Email, new { @class = "form-control", placeholder = "Email..." })
    
                    <button type="submit">Submit</button>
                }
            }
    

    SurfaceController:

    namespace package.Controllers
    {
        public class InsightsSignupController : Umbraco.Web.Mvc.SurfaceController
        {
            [HttpGet]
            public ActionResult RenderForm()
            {
                InsightsSignupModel model = new InsightsSignupModel() { FormPageId = CurrentPage.Id };
                return PartialView("InsightsSignupSurface", model);
            }
    
            [HttpPost]
            public ActionResult RenderForm(InsightsSignupModel model)
            {
    
                return PartialView("InsightsSignupSurface", model);
            }
    
            [HttpPost]
            public ActionResult SubmitForm(InsightsSignupModel model)
            {
                bool success = false;
    
                if (ModelState.IsValid)
                {
                    success = true;
                }
    
                if (success)
                {
                    TempData["value"] = "returned";
                }
    
                return PartialView("~/Views/Partials/InsightsSignupSurface.cshtml");
    
            }
        }
    }
    

    Thanks, Istvan

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Mar 22, 2020 @ 22:23
    Marc Goodson
    100

    Hi Istvan

    I'm wondering if the issue is with the creation of the form...

    There is an extension method called BeginUmbracoForm (instead of BeginForm) which adds an additional hidden field which assists in the routing of the request to your surface controller...

    using(Html.BeginUmbracoForm

    Regards

    Marc

  • Istvan Pszota 59 posts 191 karma points
    Mar 23, 2020 @ 06:26
    Istvan Pszota
    0

    Hi Marc!

    Many thanks! That was the problem.

    I can't tell you how many times i have checked the code and compared it with other solutions, but were not able to spot that.. :)

    Thanks again!

    Istvan

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Mar 23, 2020 @ 07:35
    Marc Goodson
    0

    Great!,

    I used to run the L2 Umbraco Training course... so once a month I'd look over the shoulders of ten students as they typed in the wiring up of their first ever Surface Controller... have seen a lot of the variations of how the code can look right but still not work :-P

    The other thing to be aware of is that once you 'post' to a SurfaceController, then you are sitting on the 'Surface' of the MVC controller that is handling the route, so it's difficult to return a Partial View directly from the POST action - but Umbraco provides several helper actions:

    If you return CurrentUmbracoPage();eg if modelstate is invalid and you want to show validation errors - this will return whatever page the form was inserted on, with the ModelState intact, and therefore you get to see form with the text the user has entered so far, and any validation errors.

    If everything is ok 'validation' wise, and you've handled the form submission (sent that email, added the details to a database table or whatever) then you can return RedirectToCurrentUmbracoPage(); this will redirect the user back to the original page they encountered the form on, but all ModelState will be cleared, eg their entries will be gone, and you can use the Temp dictionary to display a message, or hide the form.

    https://our.umbraco.com/Documentation/Getting-Started/Code/Creating-Forms/#adding-the-controller

    There is also a helper if you want to redirect to a specific page after the form is successfully submited, if you know the id of the Page:

    return RedirectToUmbracoPageResult(pageId);
    

    regards

    Marc

  • Istvan Pszota 59 posts 191 karma points
    Mar 23, 2020 @ 17:22
    Istvan Pszota
    0

    Hey Marc!

    Thanks for the additional help and care on this!

    I will rewrite it the way you suggested it.

    Thanks again, Istvan

Please Sign in or register to post replies

Write your reply to:

Draft