转载自:https://www.cnblogs.com/for-easy-fast/p/12790507.html;
1. 登录界面
login.css
body, html { height: 100%; } .layui-admin-login-header { padding: 20px; text-align: center; } .layui-admin-login-header h1 { color: #009688; font-family: "Helvetica Neue, Helvetica, PingFang SC, 微软雅黑, Tahoma, Arial, sans-serif"; font-weight: bold; margin-bottom: 10px; } .layui-admin-login-header p { color: white; font-family: "Helvetica Neue, Helvetica, PingFang SC, 微软雅黑, Tahoma, Arial, sans-serif"; font-weight: bold; margin-bottom: 10px; } .layui-admin-login { width: 24%; margin: 0 auto; position: absolute; top: 20%; left: 37%; box-shadow: 0 -15px 30px #0d957a; border-radius: 5px; padding: 2%; z-index: 1000; } .layui-admin-login .layui-footer { color: white; } .layui-admin-login .layui-form-checkbox { margin-left: 0px; } /*chrome浏览器透明有点问题*/ .layui-form label, input { background-color: transparent !important; color: white !important; }
login.cshtml页面
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
2. 导航界面
index.cshtml
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|
3. 内容页面
3.1 查询
Student2Tab.cshtml
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
3.2 新增
Add.cshtml
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
3.3 修改
Edit.cshtml
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
3.4 删除
此外 layui 自带集成了一些功能
4. 后台代码
Student2Constroller
using StudentMvc.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace StudentMvc.Controllers { public class Student2Controller : Controller { private void DataInit() { using (EFDbContext dbContext = new EFDbContext()) { for (int i = 1; i <= 30; i++) { Student Student2 = new Student(); Student2.Name = "张三" + i; Student2.Pwd = "123456"; Student2.Sex = "男"; Student2.StuNo = i; Student2.BrithDate = DateTime.Now; Student2.Address = "武汉江夏"; dbContext.Student.Add(Student2); } for (int i = 1; i <= 30; i++) { Student2 Student2 = new Student2(); Student2.Name = "张三" + i; Student2.Pwd = "123456"; Student2.Sex = "男"; Student2.StuNo = i; Student2.BrithDate = DateTime.Now; Student2.Address = "武汉江夏"; dbContext.Student2.Add(Student2); } dbContext.SaveChanges(); } } /// <summary> /// 学生列表数据 /// </summary> /// <param name="page"></param> /// <param name="limit"></param> /// <returns></returns> public ActionResult Student2List(int page, int limit,int? id) { //DataInit(); using (EFDbContext dbContext = new EFDbContext()) { List<Student2> Student2List= new List<Student2> (); if (id !=null) { Student2List= dbContext.Student2.Where(s=>s.Id==id).OrderBy(s => s.Id).Skip(limit * (page - 1)).Take(limit).ToList(); //总共多少数据 var allCount = Student2List.Count(); //把totle和rows:[{},{}]一起返回 //先建立一个匿名类 var dataJson = new { code = 0, msg = "", count = allCount, data = Student2List }; return Json(dataJson, JsonRequestBehavior.AllowGet); } else { Student2List = dbContext.Student2.OrderBy(s => s.Id).Skip(limit * (page - 1)).Take(limit).ToList(); //总共多少数据 var allCount = dbContext.Student2.Count(); //把totle和rows:[{},{}]一起返回 //先建立一个匿名类 var dataJson = new { code = 0, msg = "", count = allCount, data = Student2List }; return Json(dataJson, JsonRequestBehavior.AllowGet); } } } // GET: Student2 /// <summary> /// 导航页面 /// </summary> /// <returns></returns> public ActionResult Index() { int userId; if (Session["userId"] == null || !int.TryParse(Session["userId"].ToString(), out userId)) { return Redirect("~/Student2/Login"); } EFDbContext dbContext = new EFDbContext(); var user = dbContext.Student2.Find(userId); return View(user); } /// <summary> /// 弹框编辑页面 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult Edit(int id) { EFDbContext dbContext = new EFDbContext(); Student2 stu = dbContext.Student2.Find(id); ViewBag.id = id; return View(); } /// <summary> /// 弹框新增页面 /// </summary> /// <returns></returns> public ActionResult Add() { return View(); } /// <summary> /// 编辑页面的初始数据 /// </summary> /// <param name="id"></param> /// <returns></returns> public JsonResult EditInitData(int id) { EFDbContext dbContext = new EFDbContext(); Student2 stu = dbContext.Student2.Find(id); return Json(stu, JsonRequestBehavior.AllowGet); } /// <summary> /// 提交编辑页面的数据 /// </summary> /// <param name="stu"></param> /// <returns></returns> public ActionResult UpdateStudent2(Student2 stu) { EFDbContext dbContext = new EFDbContext(); var s = dbContext.Student2.Find(stu.Id); if (stu.StuNo != s.StuNo && dbContext.Student2.Where(m => m.StuNo == stu.StuNo).FirstOrDefault() != null) { return Content("error:学号已存在,请修改后再操作!"); } s.Name = stu.Name; s.Pwd = stu.Pwd; s.Sex = stu.Sex; s.StuNo = stu.StuNo; s.BrithDate = stu.BrithDate; dbContext.SaveChanges(); return Content("ok:修改成功"); } /// <summary> /// 提交新增页面的数据 /// </summary> /// <param name="data"></param> /// <returns></returns> public ActionResult AddStudent2(Student2 data) { EFDbContext dbContext = new EFDbContext(); if (dbContext.Student2.Where(m => m.StuNo == data.StuNo).FirstOrDefault() != null) { return Content("error:学号已存在,请修改后再操作!"); } dbContext.Student2.Add(data); dbContext.SaveChanges(); return Content("ok:新增成功"); } /// <summary> /// 提交要删除的数据 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult DeleteStudent2(int id) { EFDbContext dbContext = new EFDbContext(); var s = dbContext.Student2.Find(id); dbContext.Student2.Remove(s); dbContext.SaveChanges(); return Content("ok:删除成功"); } /// <summary> /// 学生列表页面 /// </summary> /// <returns></returns> public ActionResult Student2Tab() { return View(); } /// <summary> /// 登录页面 /// </summary> /// <returns></returns> public ActionResult Login() { return View(); } /// <summary> /// 导航页登出 /// </summary> /// <returns></returns> public ActionResult LogOut() { Session["userId"] = null; return Redirect("~/Student2/Login"); } /// <summary> /// 登录验证 /// </summary> /// <param name="txtName"></param> /// <param name="txtPwd"></param> /// <returns></returns> public ActionResult CheckLogin(string txtName, string txtPwd) { string userName = txtName; string userPwd = txtPwd; EFDbContext dbContext = new EFDbContext(); var Student2 = dbContext.Student2.Where(s => s.Name == userName && s.Pwd == userPwd).FirstOrDefault(); if (Student2 != null) { Session["userId"] = Student2.Id; return Content("ok:登录成功"); } else { return Content("no:用户名或者密码错误"); } } } }
以上有些没贴到代码的地方,请我的上一篇博文 MVC5+EasyUI+EF6增删改查以及登录登出的演示 里面找
经过几天的折腾,终于将一个完整的demo弄出来了,这几天走了一些弯路,
总结经验教训如下:
1. 最开始,还是老实地参考官网的示例,一个个去了解它各个模块的使用,而不是去网上搬别人的源码,然后在上面修改(我就是这样 ,耗时耗力 ,大半地时间的耗在了前面。 结果~~~ )。
-
前端框架在不断的升级,别人那时候用的版本和你现在用的版本不一致,有时候,别人解决bug的方法不一定适用于你,最好的办法是 参考别人的思路,然后对着官网说明 ,自己手写处理方法
-
学习新框架中,最开始不要去做笔记,而要多去看示例,多操练示例,次数多了你就明白它是怎么回事。等你熟到一定程度就可以做笔记了。
项目源码地址
有兴趣研究的。可以进QQ群,在群在线文档里面进行下载。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
下面是,学习使用layui时,收集整理的一些资料,希望对大伙有所帮助吧,后期再进行更新~
链接:https://pan.baidu.com/s/1Y1ktqKMAl9T2SXwpVKGaHw
提取码:76h3
仅做自己学习收藏