0

In my share _layout.cshtml, I make a call to @Html.Partial("~/Views/Account/Register.cshtml") when the user is not authenticated. This page is a Register-form. My Problem is that the httpPost with the data is not being 'catched' because (i think) de included partial view uses another controller. I just started with MVC 4 and this is all really confusing.. Any advice for me?

_Layout.cshtml

 <div id="content">
    @RenderBody()
    @if (!Request.IsAuthenticated) {
             @Html.Partial("~/Views/Account/Register.cshtml") 
    }
 </div>

AccountController

// GET: /Account/Register
 [AllowAnonymous]
    public ActionResult Register()
    {
        return View();
    }



//
 // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                WebSecurity.Login(model.UserName, model.Password);
                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

I think the problem is that since I load in the register.cshtml as a partial view, the httpPost is not sent from /Account/Register but from Home/Index. COuld this be the case?

1 Answer 1

1

Your guess sounds correct. In your register.cshtml you can do this with your form declaration:

@Html.BeginForm("Register","Register")

to cause the correct controller / view to be called.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.