【问题标题】:ASP MVC 4 open partialView() and HttpPost (controller issue)ASP MVC 4 打开 partialView() 和 HttpPost(控制器问题)
【发布时间】:2013-03-10 23:04:05
【问题描述】:

在我的共享 _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 发送的。会是这样吗?

【问题讨论】:

    标签: c# post asp.net-mvc-4 controller


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      相关资源
      最近更新 更多