0

私の共有_layout.cshtmlでは、ユーザーが認証されていないときに@ Html.Partial( "〜/ Views / Account / Register.cshtml")を呼び出します。このページは登録フォームです。私の問題は、含まれている部分ビューが別のコントローラーを使用しているため、データを含むhttpPostが「キャッチ」されていないことです。私はMVC4を始めたばかりですが、これはすべて本当に混乱しています。私に何かアドバイスはありますか?

_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から送信されることだと思います。これは事実でしょうか?

4

1 に答える 1

1

あなたの推測は正しいように聞こえます。register.cshtmlで、フォーム宣言を使用してこれを行うことができます。

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

正しいコントローラー/ビューが呼び出されるようにします。

于 2013-03-10T23:11:03.477 に答える