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 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...
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.
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:
SurfaceController:
Thanks, Istvan
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
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
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:
regards
Marc
Hey Marc!
Thanks for the additional help and care on this!
I will rewrite it the way you suggested it.
Thanks again, Istvan
is working on a reply...