我有一个使用partialView进行登录的共享布局。 如果登录过程成功,它会重定向到主页,一切正常,但如果登录失败, 它仅返回带有错误的部分视图,而不包含共享布局。
结果是一个包含此网址的网页:localhost:35967/Account/Login
,只有部分视图包含验证摘要错误。
如何解决?
这是我的代码:
AccountController.cs
[HttpGet]
public ActionResult Login()
{
return PartialView();
}
[AllowAnonymous]
[HttpPost]
public ActionResult Login(LoginModel login,string returnUrl)
{
if (ModelState.IsValid)
{
if (ValidateUser(login))
{
FormsAuthentication.SetAuthCookie(login.Username, false);
return Redirect(returnUrl ?? Url.Action("Index", "Home"));
}
ModelState.AddModelError("", "User not found");
}
return PartialView(login);
}
Login.cshtml
@model DesignCrafts.MVC.Models.LoginModel
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { id = "login", @class = "enter_form" }))
{
<span style="color:red">@Html.ValidationSummary(true, "Login failed")</span>
@Html.TextBoxFor(model => model.Username, new { @placeholder = "username" })
@Html.ValidationMessageFor(model => model.Username)
@Html.PasswordFor(model => model.Password, new { @placeholder = "password" })
@Html.ValidationMessageFor(model => model.Password)
<input type="submit" value="login" class="btn" />
<span style="margin-right:5px">
@Html.ActionLink("register", "Register", "Account", new { @class = "white-link"})
</span>
@Html.ValidationSummary(true, "user not found")
}
_Layout.cshtml
@if (Request.IsAuthenticated)
{
<div class="enter">
<div>
Logged as: @User.Identity.Name
</div>
@Html.ActionLink("Log out", "Logoff", "Account", null, new { @class = "exit" })
</div>
}
else
{
{ Html.RenderAction("Login", "Account"); }
}