asp.net mvc 三层加EF 登录注册 增删改查

  • 首先打开vs软件
  • 新建项目
  • 创建web中的mvc项目
  • 再右击解决方案创建类库项目
  • 分别创建DAL层和BLL层再把DAL层和BLL层的类重命名
  • 在mvc项目中的Models文件夹创建model类
  • 在DAL创建ADO.NET实体数据模型后把DAL层中App.Config文件中的链接字符串复制到mvc项目的Web.config文件中
  • ADO.NET实体数据模型
  • asp.net mvc 三层加EF 登录注册 增删改查_第1张图片
  • asp.net mvc 三层加EF 登录注册 增删改查_第2张图片
  • DAL层中的类开始打代码
  • 登录
    DAL层
 /// 
        /// 登录
        /// 
        /// 登录名
        /// 是否停用
        /// 密码
        /// 
        public static int Login(string studentname, string phone)
        {
            using (zzqEntities1 db = new zzqEntities1())
            {
                int stu = db.student.Where(s => s.Studentname == studentname && s.Studentaddress =="启用" && s.phone == phone).Count();
                return stu;
            }
        
        }

查询

   /// 
        /// 查询
        /// 
        /// 
        public static  List  studentSelect() { 
            using (zzqEntities1 db = new zzqEntities1()) {
                List stu = new List();
                stu = db.student.ToList();
                return stu;
            }
         
        }

添加

 /// 
        /// 添加
        /// 
        /// 姓名
        /// 是否停用
        /// 密码
        /// 
        public static int Insert(string studentname, string studentaddress, string phone) {
            using (zzqEntities1 db = new zzqEntities1())
            {
                var stu = new student() { Studentname = studentname, Studentaddress = studentaddress, phone = phone };
                db.student.Add(stu);
                return db.SaveChanges(); 
            }
        
        }

删除

  /// 
        /// 删除
        /// 
        /// 编号
        /// 
        public static int Delete(int id)
        {
            using (zzqEntities1 db = new zzqEntities1())
            {
                var stu = new student() { Studentid = id };
                db.student.Attach(stu);
                db.student.Remove(stu);
                return db.SaveChanges();
            }
        }

修改

  /// 
        /// 查询编号
        /// 
        /// 编号
        /// 
        public static List updateSelect(int id)
        {
            using (zzqEntities1 db = new zzqEntities1())
            {
                var st = db.student.Where(x => x.Studentid == id).ToList();
                return st;
            }
        }
   /// 
        /// 修改
        /// 
        /// 编号
        /// 姓名
        /// 是否停用
        /// 密码
        /// 
        public static int update(int id, string studentname, string studentaddress, string phone)
        {
            using (zzqEntities1 db = new zzqEntities1())
            {
                var st = db.student.Where(x => x.Studentid == id).FirstOrDefault();
                st.Studentid = id;
                st.Studentname = studentname;
                st.Studentaddress = studentaddress;
                st.phone = phone;
                return db.SaveChanges();
            }
        
        }

BLL层

using DAL;
引用DAL层

登录

   /// 
        /// 登录
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int Login(string studentname, string phone)
        {
            try
            {
                int count = kaoshiDAL.kaoshidal.Login(studentname, phone);
                return count;
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

查询

/// 
       /// 查询
       /// 
       /// 
        public static List studentSelect() {
            try
            {
                return kaoshiDAL.kaoshidal.studentSelect();
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        
        }

添加

   /// 
        /// 添加
        /// 
        /// 姓名
        /// 是否停用
        /// 密码
        /// 
        public static int Insert(string studentname, string studentaddress, string phone) {
            try
            {
                return kaoshiDAL.kaoshidal.Insert(studentname,studentaddress,phone);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        
        }

删除

  /// 
        /// 删除
        /// 
        /// 
        /// 
        public static int Delete(int id)
        {
            try
            {
                return kaoshiDAL.kaoshidal.Delete(id);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        
        }

修改

 /// 
        /// 查询编号
        /// 
        /// 
        /// 
        public static List updateSelect(int id)
        {
            try
            {
                return kaoshiDAL.kaoshidal.updateSelect(id);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }
         /// 
        /// 修改
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int update(int id, string studentname, string studentaddress, string phone)
        {
            try
            {
                return kaoshiDAL.kaoshidal.update(id, studentname, studentaddress, phone);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

mvc项目中的Models文件夹的model类

using DAL;
using BLL;
这里引用DAL层和BLL层

登录

  /// 
        /// 登录
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int Login(string studentname, string phone)
        {
            try
            {
              int count= kaoshiBLL.kaoshibll.Login(studentname, phone);
              return count;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

查询

  /// 
        /// 查询
        /// 
        /// 
        public static List studentSelect()
        {
            try
            {
                return kaoshiBLL.kaoshibll.studentSelect();
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

添加

  /// 
        /// 添加
        /// 
        /// 姓名
        /// 是否停用
        /// 密码
        /// 
        public static int Insert(string studentname, string studentaddress, string phone) {
            try
            {
                return kaoshiBLL.kaoshibll.Insert(studentname,studentaddress,phone);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        
        }

删除

 /// 
        /// 删除
        /// 
        /// 
        /// 
        public static int Delete(int id)
        {
            try
            {
                return kaoshiBLL.kaoshibll.Delete(id);
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

修改

/// 
        /// 查询编号
        /// 
        /// 
        /// 
        public static List updateSelect(int id)
        {
            try
            {
                return kaoshiBLL.kaoshibll.updateSelect(id);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        /// 
        /// 修改
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int update(int id, string studentname, string studentaddress, string phone)
        {
            try
            {
                return kaoshiBLL.kaoshibll.update(id, studentname, studentaddress, phone);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

在mvc项目中的Controllers文件夹创建Home控制器

using Kaoshi.Models;
using kaoshiDAL;
using kaoshiBLL;

登录

   /// 
        /// 登录
        /// 
        /// 
        public ActionResult Login()
        {
            
            return View();
        }
        public ActionResult ALogin(string studentname,  string phone)
        {
            int count = kaoshiModel.Login(studentname, phone);
            if (count >0)
            {
                return Content("");
                  
            }
            else
            {
                return Content("");
            }
            
        }

查询

   /// 
        /// 查询
        /// 
        /// 
        /// 
        public ActionResult Index()
        {
            List stu = kaoshiModel.studentSelect();
            return View(stu);
        }

注册

 /// 
        /// 注册
        /// 
        /// 
        public ActionResult Zhuce()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Azhuce(string studentname, string studentaddress, string phone)
        {
            int  st = kaoshiModel.Insert(studentname,studentaddress,phone);
            if (st > 0)
            {
                return Content("");
            }
            else {
                return Content("");
            }
        }

删除

 /// 
        /// 删除
        /// 
        /// 
        /// 
        public ActionResult Delete(int id)
        {
            int st = kaoshiModel.Delete(id);
            if (st > 0)
            {
                return Content("");
            }
            else
            {
                return Content("");
            }
        }

修改

   //修改
        public ActionResult Update(int id)
        {
            List li = kaoshiModel.updateSelect(id);
            return View(li);
        }
        public ActionResult updatestudent(int id, string studentname, string studentaddress, string phone)
        {
            int st = kaoshiModel.update(id, studentname, studentaddress, phone);
            if (st>0)
            {
                 return Content("");
            }
            else
            {
                return Content("");
            }
        }

Index视图

@{
    ViewBag.Title = "Index";
}
 
 登录

    @foreach (var item in Model)
    {
    
    }
姓名 密码 是否停用 操作
@item.Studentname @item.phone @item.Studentaddress 修改 删除

Login视图

@{
    ViewBag.Title = "Login";
}

登录

@using (Html.BeginForm("ALogin", "Home", FormMethod.Post)) { 注册 }

Update视图

@{
    ViewBag.Title = "Update";
}
 
@using (Html.BeginForm("updatestudent", "Home", FormMethod.Post)) { 

    @foreach (var item in Model)
	{
		 
	}
     
编号:
姓名:
是否停用:
密码:
}

Zhuce视图

@{
    ViewBag.Title = "Zhuce";
}
 
@using (Html.BeginForm("Azhuce", "Home", FormMethod.Post)) { 

    
姓名:
状态:
密码:
}

你可能感兴趣的:(ASP.NET,MVC)