第一步,在SqlServer数据库中创建存储过程,查询的是用户名(员工姓名)所扮演的角色:

if exists(select * from sys.objects where name='proc_select_Login')
begin
drop procedure proc_select_Login
end
go
create procedure proc_select_Login
@ename nvarchar(), --用户名
@epwd nvarchar() --密码
as
select j.JName from EmployInfo,JobInfo j where EName=@ename and EPwd=@epwd
go

第二步,在Model中业务逻辑EmployeeDao类下创建SelectLogin方法,使用ExecuteScalar()方法查询出用户名(员工姓名)的角色,返回的是字符串类型:

 /// <summary>
/// 管理员登录
/// </summary>
/// <param name="ename"></param>
/// <param name="epwd"></param>
/// <returns></returns>
public string SelectLogin(string ename, string epwd)
{
string sql = "proc_select_Login";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ename", ename);
cmd.Parameters.AddWithValue("@epwd", epwd);
con.Open();
string jName = string.Empty;
jName = Convert.ToString(cmd.ExecuteScalar());
con.Close();
return jName;
}

第三步,在Controller控制器中创建名称为EmployeeController的.cs文件,调用EmployeeDao类下SelectLogin()方法,进行员工登录验证。登录成功,进行页面跳转,并把用户名(员工姓名)和其角色存储到Session中,

注意:string txtCheck 是接受页面中写入的验证码,且与页面中验证码input标签中的name必须一致。

/// <summary>
/// 员工登录验证
/// </summary>
/// <param name="collection"></param>
/// <param name="txtCheck">验证码</param>
/// <returns></returns>
[HttpPost]
public ActionResult Login(FormCollection collection,string txtCheck)
{ //接收页面输入的用户名和密码
employ.EName = collection["txtname"];
employ.EPwd = collection["txtpwd"]; string str = Convert.ToString(this.TempData["Code"]).ToLower();//接收存在临时数据中的验证码
string jName = dao.SelectLogin(employ.EName, employ.EPwd); if (jName!=string.Empty)
{
if (str != txtCheck.ToLower())
{
ViewBag.ErrorMsg = "验证码错误";
return View(employ);
}
else
{
//使用Session进行储存
this.Session["eName"] = employ.EName; //使用session进行储存
this.Session["jName"] = jName; //this.Session.Add("employ", employ);
return RedirectToAction("Index", "Home");
}
}
else
{
ViewBag.ErrorMsg = "用户名或密码错误";
return View(employ);
}
}
}

第四步,创建登录页面,我这里嵌入的是html代码:

<body>
<form action="/Employee/Login" method="post">
<table border="" align="center" cellpadding="" cellspacing="" style=" margin-top:150px;">
<tr>
<td width="31%" height="" class="login-text02">
用户名:<br />
</td>
<td width="69%">
<input type="text" name="txtname" id="txtname" @*value="@Model.EName" *@/>
</td>
</tr>
<tr>
<td height="" class="login-text02">
密 码:<br />
</td>
<td>
<input type="password" name="txtpwd" id="txtpwd" @*value="@Model.EPwd"*@ />
</td>
</tr>
<tr>
<td height="" class="login-text02">
验证码:<br />
</td>
<td>
<div style="width:400px;">
<input type="text" name="txtCheck" />&nbsp;&nbsp;&nbsp;
<img id="valiCode" style="cursor: pointer;" src="/ValidateCodeImg/Get" title="看不清,点击换一张" alt="看不清?请点我" onclick="imgCodes(this);" />
&nbsp;&nbsp;
<a href="javascript:imgCodes();">看不清楚,点击这里</a> </div>
</td>
</tr>
<tr>
<td height="">
&nbsp;
</td>
<td>
<input type="submit" id="btnlogin" value="登录" class="btn btn-success"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="重 置" class="btn btn-warning"/>
<label style="color:Red;">@ViewBag.ErrorMsg</label>
</td>
</tr>
</table>
</form>
</body>

第五步,在Controller中创建控制器命名为HomeController,接受之前Session中存储的用户和其角色:

 public ActionResult Top(FormCollection collecton)
{ string eName = Convert.ToString(this.Session["eName"]);//接收存在session中的验证码 string jName = this.Session["jName"].ToString();
return View();
}

第六步,为HomeController创建页面,绑定Session中存储的存储的用户名和其角色:

<table width="100%" border="" cellspacing="" cellpadding="">
<tr>
<td>
<img src="@Url.Content("~/Content/images/uesr.gif")" width="" height="">
<span class="STYLE2">
当前登录用户:@if (Session["eName"] != null)
{
<span>@Session["eName"].ToString()</span> }&nbsp;&nbsp;&nbsp;&nbsp;
角色:@if (Session["jName"]!=null)
{
<span>@Session["jName"].ToString()</span>
}
</span>
</td>
</tr>
</table>

显示结果:

在mvc中将session的值绑定在页面上的更多相关文章

  1. jquery代码修改input的value值,而页面上input框的值没有改变的解决办法

    问题描述: 在搜索框中输入一些字符,并且点击搜索框右边的五角星做收藏操作时,打开的弹框中Save Search:后面的input中的值被赋值了外面搜索框的值,但是当此次操作完成之后,再次做同样的操作, ...

  2. Spring MVC中Session的正确用法<转>

    Spring MVC是个非常优秀的框架,其优秀之处继承自Spring本身依赖注入(Dependency Injection)的强大的模块化和可配置性,其设计处处透露着易用性.可复用性与易集成性.优良的 ...

  3. ASP.NET MVC之Session State性能问题(七)

    前言 这一节翻译一篇有关Session State性能问题的文章,非一字一句翻译. 话题 不知道我们在真实环境中是否用到了Session State特性,它主要用来当在同一浏览器发出多个请求时来存储数 ...

  4. 【转】Spring MVC中Session的正确用法之我见

    Spring MVC是个非常优秀的框架,其优秀之处继承自Spring本身依赖注入(Dependency Injection)的强大的模块化和可配置性,其设计处处透露着易用性.可复用性与易集成性.优良的 ...

  5. ASP.NET MVC中默认Model Binder绑定Action参数为List、Dictionary等集合的实例

    在实际的ASP.NET mvc项目开发中,有时会遇到一个参数是一个List.Dictionary等集合类型的情况,默认的情况ASP.NET MVC框架是怎么为我们绑定ASP.NET MVC的Actio ...

  6. Asp.net Mvc 自定义Session (二)

    在 Asp.net Mvc 自定义Session (一)中我们把数据缓存工具类写好了,今天在我们在这篇把 剩下的自定义Session写完 首先还请大家跟着我的思路一步步的来实现,既然我们要自定义Ses ...

  7. MVC之Session State性能

    ASP.NET MVC之Session State性能问题(七)   前言 这一节翻译一篇有关Session State性能问题的文章,非一字一句翻译. 话题 不知道我们在真实环境中是否用到了Sess ...

  8. Spring MVC中Session的正确用法之我见

    Spring MVC是个非常优秀的框架,其优秀之处继承自Spring本身依赖注入(Dependency Injection)的强大的模块化和可配置性,其设计处处透露着易用性.可复用性与易集成性.优良的 ...

  9. MVC框架中的值提供机制(三)

    在MVC框架中NameValueCollectionValueProvider采用一个NameValueCollection作为数据源,DictionnaryValueProvider的数据源类型自然 ...

随机推荐

  1. 16进制ascii码转化为对应的字符,付ipmitool查询硬件信息

    最近工作需要在用ipmitool查询服务器硬件信息.ipmitool查询硬件信息 比如电源,使用命令: 获取PSU0信息:Ipmitool raw 0x3a 0x71 0x00: 获取PSU1信息:I ...

  2. 【XLL 框架库函数】 QuitFramework

    去初使化框架库,简问题是才的重新初使化 XLOPER/XLOPER12. 参数 这个函数没有参数 属性值/返回值 这个函数没有返回值.

  3. 2. iOS程序的生命周期

    程序启动-生命周期 来自:  QQ: 853740091 1.首先讲解UIApplication对象 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个 ...

  4. CoreAnimation 之CAReplicatorLayer

    CAReplicatorLayer: 主要作用有以下两个: CAReplicatorLayer的目的是为了高效生成许多相似的图层,它会绘制一个或多个图层的子图层 并在每个复制体上应用不同的变换 使用C ...

  5. 【数据结构初学】(java实现篇)——队列(转)

    原文地址:http://www.cnblogs.com/skywang12345/p/3603935.html 原文地址:http://www.cnblogs.com/skywang12345/p/3 ...

  6. ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)

    1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 112[ ...

  7. [译]SQL Server分析服务的权限配置

    简介: 本文介绍如何配置SSAS数据库和cube相关维度的安全设置. 相对数据引擎来说,在Management Studio中配置分析服务的安全设置基本没什么区别.但是也会有一些限制,比如SSAS的权 ...

  8. 基于requests实现极客学院课程爬虫

    背景 本文主要是为了完成极客学院课程<Python 单线程爬虫>中讲师布置的实战作业. 开发环境 操作系统:windows 10 Python :Python 2.7 IDE:PyChar ...

  9. Go语言 数组

    介绍 Array 是值类型,Slice 和 Map 是引用类型.他们是有很大区别的,尤其是在参数传递的时候. 另外,Slice 和 Map 的变量 仅仅声明是不行的,必须还要分配空间(也就是初始化,i ...

  10. android 得到缩略图

    转载至 http://blog.csdn.net/dxh040431104/article/details/6667448 怎样获取图片的大小?思路很简单:首先我们把这个图片转成Bitmap,然后再利 ...