当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net mvc 三层加EF 登录注册 增删改查

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

2019年03月24日  | 移动技术网IT编程  | 我要评论

2013美国电影,白鲸的自述,pinset

首先打开vs软件
新建项目
创建web中的mvc项目
再右击解决方案创建类库项目
分别创建dal层和bll层再把dal层和bll层的类重命名
在mvc项目中的models文件夹创建model类
在dal创建ado.net实体数据模型后把dal层中app.config文件中的链接字符串复制到mvc项目的web.config文件中
dal层中的类开始打代码
登录

 /// <summary>
        /// 登录
        /// </summary>
        /// <param name="studentname">登录名</param>
        /// <param name="studentaddress">是否停用</param>
        /// <param name="phone">密码</param>
        /// <returns></returns>
        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;
            }
        
        }

查询

   /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public static  list<student>  studentselect() { 
            using (zzqentities1 db = new zzqentities1()) {
                list<student> stu = new list<student>();
                stu = db.student.tolist();
                return stu;
            }
         
        }

添加

 /// <summary>
        /// 添加
        /// </summary>
        /// <param name="studentname">姓名</param>
        /// <param name="studentaddress">是否停用</param>
        /// <param name="phone">密码</param>
        /// <returns></returns>
        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(); 
            }
        
        }

删除

  /// <summary>
        /// 删除
        /// </summary>
        /// <param name="studentid">编号</param>
        /// <returns></returns>
        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();
            }
        }

修改

  /// <summary>
        /// 查询编号
        /// </summary>
        /// <param name="studentid">编号</param>
        /// <returns></returns>
        public static list<student> updateselect(int id)
        {
            using (zzqentities1 db = new zzqentities1())
            {
                var st = db.student.where(x => x.studentid == id).tolist();
                return st;
            }
        }
   /// <summary>
        /// 修改
        /// </summary>
        /// <param name="studentid">编号</param>
        /// <param name="studentname">姓名</param>
        /// <param name="studentaddress">是否停用</param>
        /// <param name="phone">密码</param>
        /// <returns></returns>
        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层

登录

   /// <summary>
        /// 登录
        /// </summary>
        /// <param name="studentname"></param>
        /// <param name="studentaddress"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static int login(string studentname, string phone)
        {
            try
            {
                int count = kaoshidal.kaoshidal.login(studentname, phone);
                return count;
            }
            catch (exception ex)
            {
                
                throw ex;
            }
        }

查询

/// <summary>
       /// 查询
       /// </summary>
       /// <returns></returns>
        public static list<student> studentselect() {
            try
            {
                return kaoshidal.kaoshidal.studentselect();
            }
            catch (exception ex)
            {
                
                throw ex;
            }
        
        }

添加

   /// <summary>
        /// 添加
        /// </summary>
        /// <param name="studentname">姓名</param>
        /// <param name="studentaddress">是否停用</param>
        /// <param name="phone">密码</param>
        /// <returns></returns>
        public static int insert(string studentname, string studentaddress, string phone) {
            try
            {
                return kaoshidal.kaoshidal.insert(studentname,studentaddress,phone);
            }
            catch (exception ex)
            {
                throw ex;
            }
        
        }

删除

  /// <summary>
        /// 删除
        /// </summary>
        /// <param name="studentid"></param>
        /// <returns></returns>
        public static int delete(int id)
        {
            try
            {
                return kaoshidal.kaoshidal.delete(id);
            }
            catch (exception ex)
            {
                
                throw ex;
            }
        
        }

修改

 /// <summary>
        /// 查询编号
        /// </summary>
        /// <param name="studentid"></param>
        /// <returns></returns>
        public static list<student> updateselect(int id)
        {
            try
            {
                return kaoshidal.kaoshidal.updateselect(id);
            }
            catch (exception ex)
            {
                
                throw ex;
            }
        }
         /// <summary>
        /// 修改
        /// </summary>
        /// <param name="studentid"></param>
        /// <param name="studentname"></param>
        /// <param name="studentaddress"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        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层

登录

  /// <summary>
        /// 登录
        /// </summary>
        /// <param name="studentname"></param>
        /// <param name="studentaddress"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static int login(string studentname, string phone)
        {
            try
            {
              int count= kaoshibll.kaoshibll.login(studentname, phone);
              return count;
            }
            catch (exception ex)
            {

                throw ex;
            }
        }

查询

  /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public static list<student> studentselect()
        {
            try
            {
                return kaoshibll.kaoshibll.studentselect();
            }
            catch (exception ex)
            {

                throw ex;
            }

        }

添加

  /// <summary>
        /// 添加
        /// </summary>
        /// <param name="studentname">姓名</param>
        /// <param name="studentaddress">是否停用</param>
        /// <param name="phone">密码</param>
        /// <returns></returns>
        public static int insert(string studentname, string studentaddress, string phone) {
            try
            {
                return kaoshibll.kaoshibll.insert(studentname,studentaddress,phone);
            }
            catch (exception ex)
            {
                throw ex;
            }
        
        }

删除

 /// <summary>
        /// 删除
        /// </summary>
        /// <param name="studentid"></param>
        /// <returns></returns>
        public static int delete(int id)
        {
            try
            {
                return kaoshibll.kaoshibll.delete(id);
            }
            catch (exception ex)
            {

                throw ex;
            }

        }

修改

/// <summary>
        /// 查询编号
        /// </summary>
        /// <param name="studentid"></param>
        /// <returns></returns>
        public static list<student> updateselect(int id)
        {
            try
            {
                return kaoshibll.kaoshibll.updateselect(id);
            }
            catch (exception ex)
            {

                throw ex;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="studentid"></param>
        /// <param name="studentname"></param>
        /// <param name="studentaddress"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        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;

登录

   /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public actionresult login()
        {
            
            return view();
        }
        public actionresult alogin(string studentname,  string phone)
        {
            int count = kaoshimodel.login(studentname, phone);
            if (count >0)
            {
                return content("<script>alert('登录成功');window.location.href='/home/index';</script>");
                  
            }
            else
            {
                return content("<script>alert('登录失败');window.location.href='/home/login';</script>");
            }
            
        }

查询

   /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public actionresult index()
        {
            list<student> stu = kaoshimodel.studentselect();
            return view(stu);
        }

注册

 /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        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("<script>alert('注册成功');window.location.href='/home/login';</script>");
            }
            else {
                return content("<script>alert('注册失败');window.location.href='/home/zhuce';</script>");
            }
        }

删除

 /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public actionresult delete(int id)
        {
            int st = kaoshimodel.delete(id);
            if (st > 0)
            {
                return content("<script>alert('删除成功');window.location.href='/home/index';</script>");
            }
            else
            {
                return content("<script>alert('删除失败');window.location.href='/home/index';</script>");
            }
        }

修改

   //修改
        public actionresult update(int id)
        {
            list<student> 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("<script>alert('修改成功');window.location.href='/home/index';</script>");
            }
            else
            {
                return content("<script>alert('修改失败');window.location.href='/home/index';</script>");
            }
        }

index视图

@{
    viewbag.title = "index";
}
<script type="text/javascript">
    function update(id) {
        location.href = "/home/update/" + id;
    }

    function del(id) {
        if (confirm("是否删除?")) {
            
            location.href = '/home/delete/' + id;
        }
    }
</script> 
 <a href="/home/login">登录</a>
<table>
    <tr>
        <th>姓名<th>
         <th>密码</th>
         <th>是否停用</th>
        <th colspan="2">操作</th>
    </tr>
    @foreach (var item in model)
    {
    <tr>
     <td>@item.studentname</td>
        <td>@item.phone</td>
        <td>@item.studentaddress</td>
         <td><a href="#" onclick="update(@item.studentid)">修改</a></td>
         <td><a href="#" onclick="del(@item.studentid)">删除</a></td>
    </tr>
    }
</table>

login视图

@{
    viewbag.title = "login";
}

<h2>登录</h2>
 @using (html.beginform("alogin", "home", formmethod.post))
   {
       
   
           <input type ="text" name="studentname"   placeholder="请输入姓名"/> 
           <input type ="password" name="phone" placeholder="请输入密码"/>  
          
          
            <button type="submit" class="blue" >登录</button> 
             <a href="/home/zhuce">注册</a>
         }

update视图

@{
    viewbag.title = "update";
}
 
@using (html.beginform("updatestudent", "home", formmethod.post)) { 
<table>
    @foreach (var item in model)
    {
         <tr>
              <td>编号:</td>
            <td>
                <input   type="text" value="@item.studentid"  name="id" readonly="readonly"/>
            </td>
             
         </tr>
        <tr>
              <td>姓名:</td>
            <td>
                <input   type="text" value="@item.studentname"  name="studentname" readonly="readonly"/>
            </td>
             
         </tr>
        <tr>
              <td>是否停用:</td>
            <td>
                <input type="text" value="@item.studentaddress"  name="studentaddress"/>
            </td>
             
         </tr>
        <tr>
              <td>密码:</td>
            <td>
                <input  type="text" value="@item.phone"  name="phone" readonly="readonly"/>
            </td>
             
         </tr>
        
         <tr>
                <td colspan="2">
                    <input type="submit" id="btn" value="修改" /></td>
            </tr>
    }
     
</table>


}

zhuce视图

@{
    viewbag.title = "zhuce";
}
 
@using (html.beginform("azhuce", "home", formmethod.post)) { 

    <table style="width: 100%;">
        <tr>
            <td>姓名:</td>
            <td>
                <input type="text" placeholder="请输入姓名" name="studentname" />
            </td>
             
        </tr>
        <tr>
           <td>状态:</td>
            <td>
                <select name="studentaddress">
                    <option value="启用" selected="selected">启用</option>
                     <option value="禁用">禁用</option>
                </select>
            </td>  
        </tr>
        <tr>
             <td>密码:</td>
            <td>
                <input type="text" placeholder="请输入密码" name="phone" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input id="btn" type="submit" value="注册" />
            </td>
        </tr>
    </table>

}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网