在我的共享_layout.cshtml中,当用户未经过身份验证时,我会调用@ Html.Partial("〜/ Views / Account / Register.cshtml")。此页面是注册表单。我的问题是带有数据的httpPost没有被捕获'因为(我认为)de included partial view使用另一个控制器。我刚刚开始使用MVC 4,这一切都让人感到困惑。 对我有什么建议吗?
_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);
}
我认为问题在于,因为我将register.cshtml作为局部视图加载,所以httpPost不是从/ Account / Register发送的,而是从Home / Index发送的。这是真的吗?
答案 0 :(得分:1)
你的猜测听起来很正确。在register.cshtml中,您可以使用表单声明执行此操作:
@Html.BeginForm("Register","Register")
使调用正确的控制器/视图。