如何在登录表单上显示错误的用户名密码?

时间:2013-04-01 09:23:33

标签: asp.net-mvc asp.net-mvc-3 authentication

我正在开发MVC应用程序。 我已经设计了登录表单。

当用户输入正确的用户名和密码时,它会重定向到下一页,但是当用户输入错误的用户名或密码时,我想在登录表单上显示该消息,该怎么做。

这是控制器中的方法代码......

 [HttpPost]
        public ActionResult LoginUser(FormCollection oFormCollection)
        {
            string userName = oFormCollection["username"];
            string password = oFormCollection["password"];
            bool IsAccountPerson = false;
            var validEmployee = (from e in db.Employees
                                 where e.UserName == userName && e.Password == password
                                 select e).ToList();
            if (validEmployee.Count() == 1)
            {
                foreach (var v in validEmployee)
                {
                    oEmployee = v;
                    Session["LoggedEmployee"] = oEmployee;
                    Session["loggedEmpId"] = oEmployee.Id;

                    if (oEmployee.DesignationType == "Account")
                    {
                        IsAccountPerson = true;
                    }
                    else
                    {
                        IsAccountPerson = false;
                    }
                }
               if(IsAccountPerson)
                   return RedirectToAction("PaymentAdviceListForAccounts", "Account");
               else
                  return RedirectToAction("Index", "PaymentAdvice");

            }
            else
                return PartialView("Index");
        }

这是我的观点代码......

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />   
    <link href="@Url.Content("~/Content/bootstrap.css")" rel="stylesheet" type="text/css" />


    <title></title>
</head>


@using (Html.BeginForm("LoginUser","Login",FormMethod.Post))

{
    @*<div style="margin:15% 20% 20% 30%; width:35%;min-height:25%;border:1px #ACACAC solid;">*@

    <div class="container-fluid" style="padding-left:0px; margin-top:165px; margin-left:140px;">            




                <div class ="span3">
                    <label style="font-size:15px; color:#666666;  margin-top:5px;">Username</label>
                </div>

                <div class ="span6">
                    <input type="text" id="username" name="username" style="height:20px; width:100%;" />
                </div>




                <div class ="span3">
                    <label style="font-size:15px;color:#666666; margin-top:5px; ">Password</label>
                </div>

                <div class ="span6">
                    <input type="password" id="password" name="password" style="height:20px;  width:100%;"/>
                </div>





            <div class="span6" style="padding-left:15px;">
                  <input type="submit" name="submit" value="Login" class="btn btn-primary" style="margin-right:10px; height:30px; font-size:14px; width:55px;" />
             <input type="button" name="Login" value="Cancel" class="btn btn-primary" style="margin-right:20px; height:30px; font-size:14px; width:55px; padding-left:5px; padding-right:5px;" />
             </div>


        </div>
       </div>
     </div>

  </div>

}

</body>
</html>

1 个答案:

答案 0 :(得分:1)

创建新模型或使用TempData。

这是使用TempData的示例。

http://www.devcurry.com/2012/05/what-is-aspnet-mvc-tempdata.html