2013-03-10 77 views
0

在我的共享_layout.cshtml中,當用戶不在時,我打電話給@ Html.Partial(「〜/ Views/Account/Register.cshtml」)認證。此頁面是註冊表單。我的問題是,帶有數據的httpPost沒有被「捕獲」,因爲(我認爲)包含的部分視圖使用另一個控制器。我剛開始使用MVC 4,這一切都令人困惑...... 對我有何建議?ASP MVC 4打開partialView()和HttpPost(控制器問題)

_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不從/帳戶/發送註冊,但從Home/Index註冊。情況是這樣嗎?

回答

1

你的猜測聽起來正確。在您的register.cshtml中,您可以使用表單聲明執行此操作:

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

導致正確的控制器/視圖被調用。

+0

謝謝你這麼muth! – Rps 2013-03-10 23:20:22