Asp.net mvc 如何在登录表单上显示错误的用户名和密码?
我正在开发MVC应用程序。 我已经设计了登录表单 当用户输入正确的用户名和密码时,它会重定向到下一页,但当用户输入错误的用户名或密码时,我想在登录表单上显示消息,如何操作 这是控制器中的方法代码Asp.net mvc 如何在登录表单上显示错误的用户名和密码?,asp.net-mvc,asp.net-mvc-3,authentication,Asp.net Mvc,Asp.net Mvc 3,Authentication,我正在开发MVC应用程序。 我已经设计了登录表单 当用户输入正确的用户名和密码时,它会重定向到下一页,但当用户输入错误的用户名或密码时,我想在登录表单上显示消息,如何操作 这是控制器中的方法代码 [HttpPost] public ActionResult LoginUser(FormCollection oFormCollection) { string userName = oFormCollection["username"];
[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>
@使用(Html.BeginForm(“LoginUser”、“Login”、FormMethod.Post))
{
@**@
用户名
密码
}
创建新模型或使用TempData
下面是使用TempData的示例
为什么你不使用VIEW模型而不是使用一个Frand集合?不知道如何使用它…也许你应该考虑学习?在VisualStudio中启动一个新项目时,默认MVC站点中有一个运行注册/登录模块的完美示例。