zoukankan      html  css  js  c++  java
  • mvc+EF实现简单的登陆功能

    EF采用DatabaseFirst的方式处理数据

    image 

    新建一个LoginController

    [HttpGet]
            public ActionResult Login()
            {
                var UserName = Request.Cookies["UserName"] == null ? "" : Request.Cookies["UserName"].Value;
                ViewBag.UserName = UserName;
                return View();
            }
    
            public JsonResult CheckUserLogin(UserInfo userInfo)
            {
                using (EasyUIDemoDBEntities db = new EasyUIDemoDBEntities())
                {
                    //linq查询
                    var users = from p in db.UserInfo
                                where p.Name == userInfo.Name && p.Password == userInfo.Password && p.Enable == true
                                select p;
                    if (users.Count() > 0)
                    {
                        userInfo = users.FirstOrDefault();
                        return Json(new { result = "success", content = "" });
                    }
                    else
                    {
                        return Json(new { result = "error", content = "用户名密码错误,请您检查" });
                    }
                }
            }

    view视图

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Login</title>
        <script src="~/Scripts/jquery-1.7.1.min.js"></script>
         <script type="text/javascript">
            //异步实现用户的登录
            function LoginUserInfo() {
                $.ajax({
                    url: "../Login/CheckUserLogin",
                    type: "POST",
                    dataType: "json",
                    data: { "Name": $("#UserName").val(), "Password": $("#Password").val() },
                    success: function (data) {
                        if (data.result == "success") {
                           
                            //window.location.href = "Home/GetView?viewPara=Index";
                            //window.location.href = "@Url.Content("/Home/Index/")";
                            alert('success');
                            //window.location.href = "/Home/Index";
                        }
                        else {
                            alert(data.content);
                            //window.location.href = "/Login/Login/";
                        }
                    },
                    error: function (xhr, error, ex) {
                        alert("erroraaaaa");
                        window.location.href = "/Login/Login/";
                    }
                });
            }
        </script>
    </head>
    <body>
      <div id="AddUserDialog"  style=" 300px; height: 160px; padding: 10px 20px" title="EasyUIDemo用户登录" >
            <form id="ff">
                <table id="tblAdd">
                    <tr>
                        <td>
                            <label for="UserName">用户名:</label></td>
                        <td>
                            <input  type="text" id="UserName" name="UserName" value="@ViewBag.UserName" /></td>
                        <td>
                    </tr>
                    <tr>
                        <td>
                            <label for="Password">密  码:</label></td>
                        <td>
                            <input  type="text" id="Password" name="Password"  /></td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align: center; padding-top: 10px">
                            <input type="button" value="提交" id="btnLogin" onclick="LoginUserInfo();"/>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
    </html>

    注意:在AJAX中提交地址如果在controller中不定义[httpget],则会访问第一个Loing的eAction

  • 相关阅读:
    ASP.NET 2.0的页面缓存功能介绍
    第五课 主定理
    HDU 1051 Wooden Sticks
    一行代码让浏览器变编辑器
    算法概论习题1001Forest
    第七课 寻找强连通分量
    8223. Tiling a Grid With Dominoes
    迷宫
    第八课 最小生成树之Kruskal
    解决 Ubuntu 12.04 无法调节屏幕亮度的问题
  • 原文地址:https://www.cnblogs.com/ilooking/p/4092850.html
Copyright © 2011-2022 走看看