mvc+EF實現簡單的登陸功能

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   java   sp   

EF採用DatabaseFirst的方式處理資料

 

 

建立一個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="width: 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

mvc+EF實現簡單的登陸功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.