0

在我的共享 _layout.cshtml 中,当用户未通过身份验证时,我会调用 @Html.Partial("~/Views/Account/Register.cshtml") 。这个页面是一个注册表格。我的问题是带有数据的 httpPost 没有被“捕获”,因为(我认为)包含的部分视图使用了另一个控制器。我刚开始使用 MVC 4,这真的很令人困惑。对我有什么建议吗?

_Layout.cshtml

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

帐户控制器

// 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 发送的。会是这样吗?

4

1 回答 1

1

你的猜测听起来是正确的。在您的 register.cshtml 中,您可以使用表单声明执行此操作:

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

导致正确的控制器/视图被调用。

于 2013-03-10T23:11:03.477 回答