diff --git "a/\345\210\230\347\201\277/20241202-\347\274\226\350\276\221\346\223\215\344\275\234&Linq\351\233\206\346\210\220\346\237\245\350\257\242.md" "b/\345\210\230\347\201\277/20241202-\347\274\226\350\276\221\346\223\215\344\275\234&Linq\351\233\206\346\210\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..3b2ad8b85f0d1b25c9b76e00a8aea85364b8e61c --- /dev/null +++ "b/\345\210\230\347\201\277/20241202-\347\274\226\350\276\221\346\223\215\344\275\234&Linq\351\233\206\346\210\220\346\237\245\350\257\242.md" @@ -0,0 +1,181 @@ +## 编辑操作 + +### 在控制器中 +1. 根据传入的Id,从数据库中拿到**最新**的 值 +```cs +var blog = Db.Blog.FirstOrDefault(x=>x.Id == input.Id); +``` +2. 判断是否有对应的对象,有可能找到对应的id记录 + - 找得到,修改对应的对象,然后保存回数据库,返回列表页 + ```cs + if(blog != null) + { + blog.Title = input.Title; + blog.Content = input.Content; + blog.Author = input.Author; + } + ``` + - 找不到,直接返回列表页(简单操作) + + +在控制器中,两个Edit对应的代码: + +1. 对应Edit的视图 +```cs +public IActionResult Edit(int id) +{ + // 根据传入的要编辑的id,拿到要编辑的博客文章 + // 通过一定的方式,拿到那个博客 + var blog = Db.Blog.FirstOrDefault(x=>x.Id == id); + + // 返回给视图 + return View(blog); +} +``` +2. 对应Edit编辑操作页面 post请求 +```cs +[HttpPost] +[ValidateAntiForgeryToken] //判断输入的是否为空值,如果是 则提示 +public IActionResult Edit(Blog input) +{ + ... + + return View(input); +} +``` + +### 视图中 +内容与添加操作的代码相似 + +## Linq集成查询 +作用在谁的身上:集合,特别是实现了IEnumerable接口的集合上,方法参数中,一般是Lambda表达式(其实就是匿名函数) + +1. 查询单个元素 + - First() 函数中可以写查找第一个的条件,形如(t => t.Id==id)。但是如果没有第一个元素,或者没有符合条件的第一个元素,则报错 + - FirstOrDefault() 类似上面的用法,但是在没有符合条件的时候,不报错,而是返回一个null + ```cs + var blog = Db.Blog.FirstOrDefault(x=>x.Id == id); + ``` + +2. 查询多个元素 + - Where() 条件函数,可以查找符合一定条件(可多个)的元素,返回的是一个集合 + ```cs + var blog = Db.Blog.Where(x=>x.Id == id && x.Author == "张三"); + ``` + +3. 重新返回的数据类型 + - Select() 这个函数可帮助我们处理函数返回的真正内容 + ```cs + // 可重命名,也可以直接写{ x.Id, x.Author } + var blog = Db.Blog.Select(x => new { Xyz = x.Id, Abc = x.Author }); + ``` + +## 其他 +- null:没有框框 +- 空值:有框框,但没值(里面没有加入内容) + + +# 作业--综合练习 +## 从零开始,利用MVC搭建一个CRUD经典界面 +![20241202161652](sjyniuybm.hn-bkt.clouddn.com/20241202161652.png) +```html +@model List; + + +
+
+ 新增 +
+ + + + + + + + + + + + @foreach (var item in @Model) + { + + + + + + + + } + +
序号标题内容作者操作
@item.Id@item.Title@item.Content@item.Author + 编辑 + 删除 +
+
+``` +```cs +public IActionResult Index() +{ + return View(Db.Blogs); +} +``` + +## 完成新增(或者创建)的功能 +![20241202161714](sjyniuybm.hn-bkt.clouddn.com/20241202161714.png) +```html +@model Apple.Models.Blogs; + +
+
+
+
+ +
+``` +```cs +[HttpPost] +public IActionResult Create(Blogs input) +{ + var maxId = Db.Blogs.Select(t => t.Id).Max(); + input.Id = maxId + 1; + + Db.Blogs.Add(input); + return RedirectToAction("Index"); +} +``` + +## 完成修改功能 +![20241202161744](sjyniuybm.hn-bkt.clouddn.com/20241202161744.png) +```cs +public IActionResult Edit(int id) +{ + // 得到Id + var blog = Db.Blogs.FirstOrDefault(x => x.Id == id); + return View(blog); +} +[HttpPost] +public IActionResult Edit(Blogs input) +{ + // 得到Id + var blog = Db.Blogs.FirstOrDefault(x => x.Id == input.Id); + + if (blog != null) + { + blog.Title = input.Title; + blog.Content = input.Content; + blog.Author = input.Author; + } + return RedirectToAction("Index"); +} +``` +```html +@model Apple.Models.Blogs; + +
+
+
+
+ +
+```