视图代码
<h2>学生登录界面</h2>
@using (Html.BeginForm())
{
<div id="dlu">
<div class=" row">
<div class="col-lg-6">
<input type="text" placeholder="请输入账号" name="StuLoginName" class="form-control" id="StuLoginName" />
</div>
<div class="col-lg-6">
<input type="password" placeholder="请输入密码" name="StuLoginPwd" class="form-control" id="StuLoginPwd" />
</div>
</div>
<div class=" row">
<div class="col-lg-offset-2 col-lg-8">
<img src="~/img/26a0uw36.bmp" style="display:none" id="ajaxLoading" />
<input type="submit" value="登录" class="form-control btn btn-primary" />
</div>
</div>
</div>
}
存储过程
ALTER proc [dbo].[proc_IsLogin]
@sName nvarchar(20),
@sPwd nvarchar(20),
@result int output
as
if exists(select * from Student where StuLoginName=@sName and StuLoginPwd=@sPwd)
set @result=1 --表示登录成功
else
set @result=0 --表示登录失败
DAL层
/// <summary>
/// 通过用户名和账号查询学生
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int select(string name,string pwd)
{
using (ExamDBEntities1 db = new ExamDBEntities1())
{
//定义一个输出参数
ObjectParameter paral = new ObjectParameter("result", typeof(int));
//调用一下存储过程
var result = db.proc_IsLogin(name, pwd, paral);
//返回结果
return (int)paral.Value;
}
}
BLL层
public static bool CheckLogin(string name, string pwd)
{
var tea = DAL.StudentService.select(name,pwd);
if (tea == 0)
{
return false;
}
return true;
}
控制器
/// <summary>
/// 登录界面
/// </summary>
/// <returns></returns>
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(Student student)
{
if (ModelState.IsValidField("StuLoginName") && ModelState.IsValidField("StuLoginPwd"))
{
if (StuMAc.CheckLogin1(student.StuLoginName, student.StuLoginPwd))
{
Session["loginName"] = student.StuLoginName;
return RedirectToAction("Index");
}
else { return Content("<script>alert('登录失败');location.href='/Student/Login'</script>"); }
}
else return View();
}