我在我的應用程序有一個LogInOrRegister頁,調用2個兒童行動 LogInOrRegister.cshtmlMVC4 PartialViewResult返回一個視圖,而不是PartialView
@{
ViewBag.Title = "Log in";
}
@Html.Action("Login", "Account", new { returlUrl = ViewBag.ReturnUrl})
@Html.Action("Register", new { returlUrl = ViewBag.ReturnUrl})
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
的登錄PartialView是:
@model Com.WTS.Portal.Models.LoginModel
<hgroup class="title">
<h1>@ViewBag.Title</h1>
</hgroup>
<section id="loginForm">
<h2>Use a local account to log in.</h2>
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Email)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
<li>
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
</li>
</ol>
<input type="submit" value="Log in" />
</fieldset>
}
</section>
我AccountController.cs包含以下代碼:
[AllowAnonymous]
public PartialViewResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return PartialView();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public PartialViewResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.Email, model.Password, persistCookie: model.RememberMe))
{
RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return PartialView(model);
}
I看到正確的2局部視圖我GET頁面LogInOrRegister.cshtml
當我提交表單時,如果在表單中存在驗證錯誤,則顯示視圖(無佈局)而不是部分視圖應該是部分的LogInOrRegster
任何想法?
如果任何修復它,我不知道如何。除了添加'ChildActionOnly'可以防止這個特定的'Login'操作被調用。你有另外一個實際返回'ViewResult'的'Login'操作嗎?總而言之,如果您的操作返回「PartialViewResult」,那麼您將獲得的是部分視圖,即無佈局。獲得佈局的唯一方法是返回'ViewResult'。 – 2014-09-12 18:18:52
另外,請記住,您不能發佈到兒童行動。 – 2014-09-12 18:20:47