asp.net mvc 實現簡單的登錄


1、創建一個控制器   如下:

復制代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Demo.Models;    //命名空間

namespace Demo.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        DL_DemoEntities db = new DL_DemoEntities();   //模型

        public ActionResult Index()  //首頁
        {
            return View();
        }
        public ActionResult Demo404()  //登陸失敗跳轉
        {
            return View();
        }
        public ActionResult Start_Here()  //登陸成功跳轉
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(string name, string password) 
        {
             name = Request["name"];
             password = Request["password"];
            if (string .IsNullOrEmpty(name)||string .IsNullOrEmpty(password))
            {
                return Content("<script>alert('賬號密碼不能為空');location.href='Demo404'</script>");
                
            }
            else
            {
                var user = db.User.Where(u => u.admin == name && u.passid == password).SingleOrDefault();
                if (user == null)
                {
                    return Content("<script>alert('輸入有誤');location.href='Demo404'</script>");

                }
                else
                {
                    return Content("<script>location.href='Start_Here'</script>");
//登錄成功跳轉 } } } } }
復制代碼

2、視圖界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<body style= "background:url(Html/images/bg.jpg) no-repeat;" >
     <form action= "/Home/Login"  method= "post" >
         
  <div  class = "container wrap1"  style= "height:450px;" >
             <h2  class = "mg-b20 text-center" >單木不林后台登錄頁面</h2>
             <div  class = "col-sm-8 col-md-5 center-auto pd-sm-50 pd-xs-20 main_content" >
                 <p  class = "text-center font16" >用戶登錄</p>
                 <form>
                     <div  class = "form-group mg-t20" >
                         <i  class = "icon-user icon_font" ></i>
                         <input type= "text"  class = "login_input"  id= "Email1"  name= "name"  placeholder= "請輸入用戶名"  />
                     </div>
                     <div  class = "form-group mg-t20" >
                         <i  class = "icon-lock icon_font" ></i>
                         <input type= "password"  class = "login_input"  id= "Password1"  name= "password"  placeholder= "請輸入密碼"  />
                     </div>
                     <div  class = "checkbox mg-b25" >
                         <label>
                             <input type= "checkbox"  />記住我的登錄信息
                         </label>
                     </div>
                     <button type= "submit"  class = "login_btn" >登 錄</button>
                </form>
         </div><!--row end-->
     </div><!--container end-->
     </form>
    
    
</body>

  界面

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM