我有一个HomeController与索引操作显示Index.aspx视图.它有一个用户名/密码登录部分.当用户单击提交按钮时,它会发送到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中失去了Model.