简体   繁体   中英

ASP MVC 4 open partialView() and HttpPost (controller issue)

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM