Reputation: 277
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?
Upvotes: 0
Views: 715
Reputation: 3114
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.
Upvotes: 1