【发布时间】:2011-05-30 04:03:39
【问题描述】:
我有一个 HomeController 带有一个显示 Index.aspx 视图的 Index 操作。它有一个用户名/密码登录部分。当用户单击提交按钮时,它会 POST 到 AccountController 中的登录操作。
<% Html.BeginForm("Login", "Account", FormMethod.Post); %>
在该操作中,它测试用户名/密码的有效性,如果无效,则将用户发送回登录页面,并显示凭据错误的消息。
[HttpPost]
public ActionResult Login(LoginViewModel Model, string ReturnUrl)
{
User user = MembershipService.ValidateUser(Model.UserName, Model.Password);
if (user != null)
{
//Detail removed here
FormsService.SignIn(user.ToString(), Model.RememberMe);
return Redirect(ReturnUrl);
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
// If we got this far, something failed, redisplay form
return RedirectToAction("Index", "Home"); // <-- Here is the problem. ModelState is lost.
}
但问题是:ValidationSummary 始终为空白,因为在 RedirectToAction 时我们会丢失模型。
所以问题是:如何在没有重定向的情况下将用户发送到不同控制器上的操作?
【问题讨论】:
标签: asp.net-mvc validation redirect login