我在我的应用程序有一个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