Copied to clipboard

Flag this post as spam?

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


  • Briant Diehl 3 posts 73 karma points
    Nov 05, 2018 @ 22:34
    Briant Diehl
    0

    7.12.3 Using SurfaceController Child Action with Post

    I need some help understanding an exception I get when I use a surface controller using both a child and post action. enter image description here

    I have an account controller with a child action and a post action

    public class AccountController : SurfaceController
    {
        public UmbracoHelper umbracoHelper => new UmbracoHelper(UmbracoContext.Current);
        private IPublishedContent AccountPage => Umbraco.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias == "memberControls").Children.FirstOrDefault(x => x.DocumentTypeAlias == "account");
    
        public ActionResult Login()
        {
            return PartialView(new CustomLoginModel());
        }
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(CustomLoginModel model)
        {
            if (!ModelState.IsValid) return CurrentUmbracoPage();
    
            if (!Members.Login(model.Email, model.Password))
            {
                ModelState.AddModelError("loginModel", "The email or password is invalid");
                return CurrentUmbracoPage();
            }
    
            return RedirectToUmbracoPage(AccountPage.Id);
        }
    }
    

    From my login page, I'm including my form as a partial view by calling the action

    @Html.Action("Login", "Account")
    

    Now, if I submit my form correctly and I redirect to the current umbraco page or another page everything works as expected. It's when the form has an error and I return the current umbraco page that I get the error mentioned above.

    I have found that if I include the child action and the post action in different surface controllers that I can return the current umbraco page without getting the above error. While this seems to work, I'd really rather find a way to keep my logic in the same controller. I don't get why separating the child and post actions behaves as I would expect while combining them in the same controller would result in an error.

    Any insights would be appreciated. Seems like this may have been touched on briefly here: https://our.umbraco.com/forum/developers/api-questions/36614-411-Using-SurfaceController-Child-Action-with-Post#comment-132482

Please Sign in or register to post replies

Write your reply to:

Draft