首页 新闻 赞助 找找看

MVC code first AJAX

0
悬赏园豆:50 [已解决问题] 解决于 2016-07-05 16:31

做一个登录界面,前端通过AJAX传值给后台,然后后台验证输入的用户名和密码,数据库是采用的code first

问题补充:

希望大神给出后台验证数据是否正确的源码

Sola0921的主页 Sola0921 | 初学一级 | 园豆:5
提问于:2016-07-04 16:30
< >
分享
最佳答案
0

就是写一个接口用于登录操作,这里可以直接是控制器添加一个登录验证的Action,ajax请求这个Action返回相关信息即可。

收获园豆:40
CodeHsu | 大侠五级 |园豆:5468 | 2016-07-04 19:57

给个模版可以吗?我初学MVC和ajax

Sola0921 | 园豆:5 (初学一级) | 2016-07-05 08:20

@Sola0921: 比如新建一个登录控制器 LoginController,然后添加一个登录验证的Action

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using VaiHaoBlog.Models;
 7 
 8 namespace VaiHaoBlog.Controllers
 9 {
10     public class LoginController : Controller
11     {
12         #region 登录的入口
13         /// <summary>
14         /// 登录的入口
15         /// </summary>
16         /// <returns></returns>
17         public ActionResult Index()
18         {
19             if (Session["User"] != null)
20             {
21                 return Redirect("/");
22             }
23             else
24             {
25                 return View();
26             }
27         } 
28         #endregion
29 
30         #region 登陆的Action
31         /// <summary>
32         /// 登陆的Action
33         /// </summary>
34         /// <returns></returns>
35         public ActionResult Login()
36         {
37             string name = Request.Params["username"].ToString();
38             string pwd = Request.Params["password"].ToString();
39             string code = Request.Params["code"].ToString();
40             Users models = new Users();
41             Users user = new Users();
42             user = models.GetUser(name);
43             Session["User"] = user;
44             if (user != null)
45             {
46                 if (pwd == user.UserPass)
47                 {
48                     Session["User"] = user;
49                     return Content("ok");;
50                 }
51                 else
52                 {
53                     return Content("error");;
54                 }
55             }
56             else
57             {
58                 return Content("error");;
59             }
60         } 
61         #endregion
62 
63         #region 退出系统登录
64         /// <summary>
65         /// 退出系统登录
66         /// </summary>
67         /// <returns></returns>
68         public ActionResult Logout()
69         {
70             Session["User"] = null;
71             return View("Index");
72         } 
73         #endregion
74 
75     }
76 }
View Code

然后,在前端页面调用:

就是给登录按钮绑定一个点击事件,然后,在点击事件中使用ajax请求登录结果。

例如:

 1 <script type="text/javascript"> 
 2     $().ready(function () { 
 3         $('#button-login').click(function () { 
 4             if ($('#username').val() == "" || $('#password').val() == "" || $('#code').val() == "") { 
 5                 alert("用户名或密码不能为空!"); 
 6             } 
 7             else { 
 8                 $.ajax({ 
 9                     type: "POST", 
10                     url: "/Login/Login", 
11                     data: "username=" + $('#username').val() + "&password=" + $('#password').val()+ "&code=" + $('#code').val(), 
12                     success: function (msg) { 
13                         if (msg == "success") { 
14                             windows.location.href = "/index"; //如果登录成功则跳到XX界面
15                         } 
16                         if (msg == "fail") { 
17                             alert("登录失败!"); 
18                         } 
19                     }
20                     error: function () {
21 
22                     } 
23                 }); 
24             } 
25         }); 
26     }); 
27 </script> 
View Code
CodeHsu | 园豆:5468 (大侠五级) | 2016-07-05 13:31

@SeayXu: 非常感谢

Sola0921 | 园豆:5 (初学一级) | 2016-07-05 16:30
其他回答(2)
0

看到要源码...把我吓跑了.

说实话.你用vs建个web项目模版里面就有登录注册的代码

收获园豆:5
吴瑞祥 | 园豆:29449 (高人七级) | 2016-07-04 17:39

我知道这样不太好,可是我写了很多方法,加上我是初学者总是出错,有的是输入正确用户名和密码能登录,输一个错的就出错,有的直接就没验证就登录了

支持(0) 反对(0) Sola0921 | 园豆:5 (初学一级) | 2016-07-05 08:19
0

$.post("/Login",{"name":"123","pwd":"123"},function(data,status){if(data=='1'){alert("登录成功");}})

 

public ActionResult Login()

{

string name=Request["name"].toString();

string pwd=Request["pwd"].toString();

...

验证

....

if(成功) return Content("1");

else return Content("0");

}

收获园豆:5
弦断有谁听 | 园豆:20 (初学一级) | 2016-07-05 09:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册