无废话MVC入门教程九[实战一:用户注册与登陆]

本文目标

一、独立开发用户注册与登陆

本文目录

一、在视图中使用验证码

二、在视图中使用下拉列表

三、使用FormCollection接收客户端发送的数据

四、效果预览与代码下载

一、在视图中使用验证码

  MVC中的验证码即是在Control中输出一张图片显示在View上

在View的img标签中添加验证码地址“/Image/GetCheckCode/(输入验证码的地址)”,页面代码如下:

1   <img id="check_img" alt="验证码" src="/Image/GetCheckCode/" height="30" width="80" onclick="App.refreshCheckCode('check_img')" /><span><a
2             href="javascript:App.refreshCheckCode('check_img');">换一换</a></span>

Control中的接口代码方法如下:

1   public class ImageController : Controller
2     {
3         public void GetCheckCode()
4         {
5             CreateCheckCodeImage(GenerateCheckCode());
6         }
7     }

二、在视图中使用下拉列表

页面代码如下:

1    @Html.LabelFor(user => user.Residential)
2         @Html.DropDownListFor(user => user.Residential, (SelectList)ViewBag.Residential)

Control代码如下:

 1   //取出数据,并通过Helper把数据分解
 2    AddressHelper addressHelper = AddressHelper.GetInstance();
 3    addressHelper.GetResidetialItem(GetList());
 4    //使用ViewBag传到View
 5    ViewBag.Residential = addressHelper.ResidetialItem;
 6    ViewBag.FloorNo = addressHelper.FloorNoItem;
 7    ViewBag.UnitNo = addressHelper.UnitNoItem;
 8    ViewBag.DoorplateNo = addressHelper.DoorplateNoItem;
 9 
10 
11 
12    public class AddressHelper
13     {
14         //单例
15         private AddressHelper() { }
16         private static AddressHelper Instance = new AddressHelper();
17         public static AddressHelper GetInstance()
18         {
19             return Instance;
20         }
21 
22         public SelectList ResidetialItem { get; private set; }
23         public SelectList FloorNoItem { get; private set; }
24         public SelectList UnitNoItem { get; private set; }
25         public SelectList DoorplateNoItem { get; private set; }
26 
27         //获取小区列表
28         public void GetResidetialItem(List<Model.Address> AddressItem)
29         {
30             List<SelectListItem> ResidetialItem = new List<SelectListItem>();
31             List<SelectListItem> FloorNoItem = new List<SelectListItem>();
32             List<SelectListItem> UnitNoItem = new List<SelectListItem>();
33             List<SelectListItem> DoorplateNoItem = new List<SelectListItem>();
34             foreach (Model.Address address in AddressItem)
35             {
36                 if (address.Type == 1)
37                 {
38                     ResidetialItem.Add(new SelectListItem { Value = address.AddressID.ToString(), Text = address.Name });
39                 }
40                 if (address.Type == 2)
41                 {
42                     FloorNoItem.Add(new SelectListItem { Value = address.AddressID.ToString(), Text = address.Name });
43                 }
44                 if (address.Type == 3)
45                 {
46                     UnitNoItem.Add(new SelectListItem { Value = address.AddressID.ToString(), Text = address.Name });
47                 }
48                 if (address.Type == 4)
49                 {
50                     DoorplateNoItem.Add(new SelectListItem { Value = address.AddressID.ToString(), Text = address.Name });
51                 }
52             }
53             this.ResidetialItem = new SelectList(ResidetialItem.AsEnumerable(), "Value", "Text");
54             this.FloorNoItem = new SelectList(FloorNoItem.AsEnumerable(), "Value", "Text");
55             this.UnitNoItem = new SelectList(UnitNoItem.AsEnumerable(), "Value", "Text");
56             this.DoorplateNoItem = new SelectList(DoorplateNoItem.AsEnumerable(), "Value", "Text");
57         }
58     }

ViewBag.Residential:在Control与View中传递数据

@Html.DropDownListFor:接收SelectList类型的数据,取到数据后要把List转换成该类型,此例在AddressHelper进行的转换

三、使用FormCollection接收客户端发送的数据

MVC中可以使用强类型在Control与View中进行数据传递、也可以使用FormCollection接收数据

此例中我们把验证码用FormCollection向后台传递,View代码如下:

<input type="text" name="checkCode" />

Control中的代码如下:

 1         [HttpPost]//注册时处理回发
 2         public ActionResult Regedit(Model.User user, FormCollection form)
 3         {
 4             //取出数据,并通过Helper把数据分解
 5             AddressHelper addressHelper = AddressHelper.GetInstance();
 6             addressHelper.GetResidetialItem(GetList());
 7             //使用ViewBag传到View
 8             ViewBag.Residential = addressHelper.ResidetialItem;
 9             ViewBag.FloorNo = addressHelper.FloorNoItem;
10             ViewBag.UnitNo = addressHelper.UnitNoItem;
11             ViewBag.DoorplateNo = addressHelper.DoorplateNoItem;
12 
13             //校验验证码
14             if (form["checkCode"] != null && form["checkCode"].ToString() == Session["CheckCode"].ToString())
15             {
16                 //校验其他表单元素
17                 if (ModelState.IsValid)
18                 {
19                     DemoRepository.User.Add(user);
20                     MessageBox.ShowAndRedirect(this, "注册成功,请登陆!", "/User/Login");
21                 }
22             }
23             else
24             {
25                 MessageBox.Show(this, "验证码不正确!");
26             }
27             return View();
28         }

四、效果预览与代码下载

1.注册

2.登陆

3.代码下载

数据文件在文件夹根目录下,一切尽在不言中,直接看代码吧

[点击下载] 

五、补:Cookies的使用

登陆的时候注册Cookies

 1         [HttpPost]//登陆时回发处理
 2         public ActionResult Login(Model.User user)
 3         {
 4             if (ModelState.IsValid)
 5             {
 6                 Model.User newUser = Repository.User.UserLogin(user);
 7                 //检测用户名和密码
 8                 if (newUser != null)
 9                 {
10                     DateTime Expires = DateTime.Now;
11                     if (user.Remember == true)
12                         Expires = DateTime.Now.AddDays(365);
13 
14                     Dictionary<string, string> CookieValues = new Dictionary<string, string>();
15                     CookieValues.Add("UserID", newUser.UserID.ToString());
16                     CookieValues.Add("UserName", newUser.UserName);
17                     CookieHelper cookieHelper = new CookieHelper();
18 
19                     cookieHelper.SetCookie(CookieValues, Expires);
20                     Response.Redirect("/Manage/Main");
21                 }
22                 else
23                 {
24                     MessageBox.Show(this, "用户名或密码不正确!");
25                 }
26             }
27             //客户端显示
28             return View();
29         }

此处使用cookieHelper帮助类管理Cookies,类代码如下:

 1     public class CookieHelper
 2     {
 3         private string name = "User";   //Cookie名称
 4 
 5         //是否已经被创建
 6         public bool IsCreate
 7         {
 8             get
 9             {
10                 HttpCookie Cookie = HttpContext.Current.Request.Cookies[this.name];
11                 if (Cookie != null)
12                     return true;
13                 else
14                     return false;
15             }
16         }
17 
18         //设置Cookies
19         public void SetCookie(Dictionary<string, string> Values, DateTime Expires)
20         {
21             HttpCookie Cookie = new HttpCookie(this.name);
22             foreach (string key in Values.Keys)
23             {
24                 Cookie.Values.Add(key, Values[key]);
25             }
26             Cookie.Expires = Expires;
27             HttpContext.Current.Response.Cookies.Add(Cookie);
28         }
29 
30         //获取Cookie
31         public HttpCookie GetCookie()
32         {
33             return HttpContext.Current.Request.Cookies[this.name];
34         }
35 
36         //清空Cookie
37         public void ClearCookie()
38         {
39             HttpCookie Cookie = HttpContext.Current.Request.Cookies[this.name];
40             Cookie.Expires = DateTime.Now.AddDays(-1);
41             HttpContext.Current.Response.Cookies.Add(Cookie);
42         }
43     }

 鉴于@BangQ 的回复提出的疑问,关于Cookies安全问题,请具体查阅资料。

六、这个例子代码比较多,具体请下载代码查看。

版权:http://www.cnblogs.com/iamlilinfeng

posted @ 2013-03-25 23:37  李林峰的园子  阅读(31358)  评论(44编辑  收藏  举报