![](/img/trans.png)
[英]Why PartialView is not visible after HttpPost Action in ASP.NET MVC?
[英]ASP MVC 4 open partialView() and HttpPost (controller issue)
在未驗證用戶身份的情況下,我在共享_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>
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發送。 可能是這樣嗎?
您的猜測聽起來是正確的。 在您的register.cshtml中,您可以使用表單聲明來執行此操作:
@Html.BeginForm("Register","Register")
使正確的控制器/視圖被調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.