diff --git "a/\345\220\264\345\230\211\347\216\262/20241202-\345\242\236\345\210\240\346\224\271\346\270\262\346\237\223\347\224\273\351\235\242.md" "b/\345\220\264\345\230\211\347\216\262/20241202-\345\242\236\345\210\240\346\224\271\346\270\262\346\237\223\347\224\273\351\235\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..54237d761caf0f730ff0bdc121ef9dc7050fa9d4 --- /dev/null +++ "b/\345\220\264\345\230\211\347\216\262/20241202-\345\242\236\345\210\240\346\224\271\346\270\262\346\237\223\347\224\273\351\235\242.md" @@ -0,0 +1,221 @@ +# 笔记 + ++ 第一步 + + 控制台创建控制器 ++ 第二步创建视图 + + 创建同控制器名的视图文件 + ```c# + //cd ./View/ + //mkdir Blogs + //mkdir Index(写需要显示页面的内容) + + //a标签的引用: + //注意:~/css/crue.css + +
+
+
+
+ 新增 +
+
+
+ + + + + + + + + + + + + + + + + + + +
Id标题内容作者操作
1门尼《哀人》门尼 + 编辑 + 删除 +
+
+
+ ``` ++ 第三步:在Model中创建Blogs.cs + ``` + namespace Blog.Models; + + public class Blogs{ + public int Id {get;set;} + public string Title{get;set;}=null!; + + public string Content{get;set;}=null!; + public string Author{get;set;}=null!; + } + ``` + ++ 第四:使用Model中的Blogs: + ``` + + @model List; +
+
+ 新增 +
+ +
+ + + + + + + + + + + + @foreach(var item in @Model) + { + + + + + + + + } + +
Id标题内容作者操作
@item.Id@item.Title@item.Content@item.Author + 编辑 + 删除 +
+
+
+ + ``` + ++ 第五步:Model中创建Db.cs用于存储数据 +``` +namespace Blog.Models; + +public static class Db +{ + public static List Blogs{get;set;} + static Db() + { + Blogs = []; + + for(var i=1;i<10;i++) + { + var tmp = new Blogs + { + Id = i, + Title = $"永远是朋友{i}", + Content = $"假日风情{i}", + Author = $"仙仙{i}" + }; + //将Db的内容添加到内容里 + Blogs.Add(tmp); + } + } +} +``` + +# 作业 +## 效果图 +![20241204211606](www.wikzn.store/20241204211606.png) +![alt text](../吴嘉玲/图片/crue增.png) + +# 总结 +1. 创建mvc文件 +``` +dotnet new mvc -o 'Blog' +``` + +2. 控制器创建BlogsController.cs +```c# + using Microsoft.AspNetCore.Mvc; + using Blog.Models; + + namespace Blog.Controllers; + + public IActionResult Index() + { + return View(Db.Blogs); + } + //显示添加页面 + public IActionResult Add() + { + return View(); + } + //显示删除页面 + public IActionResult Delete() + { + return View(); + } + //显示编辑页面 + public IActionResult Create() + { + return View(); + } +``` + +2. 创View与控制器同名的Blogs +3. Blogs中创视图Index + - 可以在里面使用`xx`来跳转页 + - 声明`@model List`--可以提示 +4. Index中搭建CURD 的结构 +5. 优化页面 css + + 引用css link `href="~/css/base.css"` +6. Model创Blogs.cs;让视图活起来(代表数据库表模型) + + 在里面定义字段 + ``` + public class Blogs{ + public int Id{get;set;} + public string Title{get;set;}=null!; + public string Content{get;set;}=null!; + public string Author{get;set;}=null!; + } + ``` +7. 创数据库表Db.cs;模拟数据库所以用静态 + ``` + public static class Db{ + Public static List Blogs{get;set;} + //构造函数 + static Db(){ + //桶 + Blog = []//Blog = new List(); + for (var i=0;i<10;i++>){ + 化//数组话 + var tmp = new Blogs{ + //应为从0开始所以要加1 + Id=i+1, + Title = $"文字{i}", + Content = $"文字{i}", + Author = $"文字{i}" + }; + //将tmp添加到List中 + Blogs.Add(tmp); + + } + } + } + ``` +8. 在控制器return View(Db.Blogs); +9. 视图中声明`@model List;` +10. 视图中将死代码变活 +```c# +foreach(var item in @Model){ + @item.Id + @item.Title + @item.Content + @item.Author + } +``` + diff --git "a/\345\220\264\345\230\211\347\216\262/20241204\345\242\236\346\224\271linq.md" "b/\345\220\264\345\230\211\347\216\262/20241204\345\242\236\346\224\271linq.md" new file mode 100644 index 0000000000000000000000000000000000000000..0891787b79a385a508402eb953a0db9ef17e02d7 --- /dev/null +++ "b/\345\220\264\345\230\211\347\216\262/20241204\345\242\236\346\224\271linq.md" @@ -0,0 +1,117 @@ +# 笔记 +**Linq集成查询(关联Lambda)** +1. First FirstOrDefaualt 找到第一个符合条件的元素 + + First(x=>x.Id==id)返回第一个Id等于id的元素,如果都没有符合的,报错 + + FirstOrDefault(x=>x.Id=id)返回的第一个Id等于id的元素,如果都没有符合的,返回null + +2. Single SingleOrDefaulr + + Single()返回第一个元素,如果没有,报错 + + SingleOrDefault()返回第一个元素,如果没有,返回null + +3. Where + + Where(x=>x.Score>=80 && x.Sex==1)查找所以成绩大于等于80,并且性别为1的所有元素 + +4. Select + + Select(x=>new {x.Id,x.Score}) 以新的{x.Id,x.Score}对象的形式,返回新的集合 + +from XX in XX where XXX select; + +## 新增步骤 +1. 视图创建表单内容形式和类型一样 + ``` + @model Blog.Models.Blogs; + +
+
+
+
+ +
+ ``` +2. 控制器中接受请求[HttpPost] + ``` + //创建展示新增的页面 + public IActionResult Create(){ + return View(); + } + + //创建保存表单结果的Action + + [HttpPost] + public IActionResult Create(Blogs input)//将input返回Blogs(前面那个) + { + //1.已经验证表单数据可以传入 + //2. 拿到传入的数据后,一般做验证,数据验证,如果不是必填、、长度是不是符合要求、手机邮箱格式对不对 + // 3.如果符合验证规则、则保存到数据库、否则提示验证不通过 + //4.如果保存数据库成功,则重定向列表页,,若验证不通过,就显示原来的页面 + var maxId = Db.Blogs.Select (X=>X.Id).Max(); + //var maxId = Db.Blogs.First(); + //var maxId = Db.Blogs.FirstOrDefaualt();//有机返回null + //var maxId = Db.Blogs.Single(X=>X.Id>0); + //var maxId = Db.Blogs.SingleOrDefaulr(X=>X.Id>0); + //var maxId = Db.Blogs.Where (X=>X.Id>0 && x.Author.Content('王某某')); + //var maxId = Db.Blogs.Select (X=>X.Id>0 && x.Author.Content('王某某'));//重新组织新的元素形式返回 + Db.Blogs.Add(input); + return input; + } + ``` + +3. 怎么创建Id的最大值(集成查询),在控制器请求的Create中写;Lambda:匿名函数 + + ```c# + //通过select 拿到集合中的所有id,放在一个新的集合中返回,然后对这个返回的新的集合应用Max方法,找到其中最大值 + // var blogs = Db.Blogs.Where(x => x.Title.Equals(input.Title)); + // if (blogs.Count() > 0) + // { + // return View("create"); + // } + [HttpPost] + public IActionResult Create(Blogs input){ + var maxId = Db.Blogs.Select(x=>x.Id).Max(); + input.Id=maxId+1; + + Db.Blogs.Add(input); + return RedirectToAction("Index"); + } + ``` + +## 编辑步骤 +1. Bianji视图中创建 +```c# +@model Blog.Models.Blogs; +//控制器Blogs +
+
+
+
+
+ +
+``` +2. BlogsController.cs中写 +```c# +//用于显示编辑页面 + public IActionResult Bianji(int id) + { + // 根据id找到对应的blogs,有可能为空 + var blog = Db.Blogs.FirstOrDefault(x => x.Id == id); + return View(blog); + } + + [HttpPost] + public IActionResult Bianji(Blogs input) + { + // 根据id找到对应的blogs,有可能为空 + 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; + + } + //编辑好返回Index + return RedirectToAction("Index"); + } +``` + diff --git "a/\345\220\264\345\230\211\347\216\262/20241205\345\210\240\351\231\244.md" "b/\345\220\264\345\230\211\347\216\262/20241205\345\210\240\351\231\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..201976786f3c2a46a008b7a0a38afe5700893bbe --- /dev/null +++ "b/\345\220\264\345\230\211\347\216\262/20241205\345\210\240\351\231\244.md" @@ -0,0 +1,79 @@ +# 步骤 + +## 删除步骤 +1. 需要再Index.cshtml页面中`删除`获取Id值 +2. Delete.cshtml中的样式 + ``` + @model Blog.Models.Blogs; +

你真的要删除吗?

+ + + + + + + + + + + + + + + + + +
标题@Model.Title
内容@Model.Content
作者@Model.Author
删除回到主页
+ ``` +3. 展示删除页面 + ``` + public IActionResult Delete(int id) + { + //根据id找到对应的blog + var blog=Db.Blogs.FirstOrDefault(X=>X.Id==id); + return View(blog); + } + ``` +4. 确认删除页面 + ``` + public IActionResult DeleteConfirm(int id) + { + //根据id找到对应的blog + var blog=Db.Blogs.FirstOrDefault(X=>X.Id==id); + //如果blog不为空,查找出来就删除 + if(blog!=null) + { + Db.Blogs.Remove(blog); + //删除完返回列表页 + return RedirectToAction("Index"); + } + //如果找不到就返回找不到提示 + return NotFound(); + } + ``` + + +## 查询步骤 +1. Index.cshtml的查询样式 + ``` +
+ + +
+ ``` +2. 查询页面 + ``` + public IActionResult Index(string keyword) + { + //判读keyword是否为空,不为空则删除空格 + keyword=string.IsNullOrEmpty(keyword)?"":keyword.Trim(); + //如果为空返回主页 + if(string.IsNullOrEmpty(keyword)) + { + return View(Db.Blogs); + } + //如果不为空,则查询 + var list=Db.Blogs.Where(x=>x.Title.Contains(keyword)||x.Content.Contains(keyword)||x.Author.Contains(keyword)).ToList(); + return View(list); + } + ``` \ No newline at end of file diff --git "a/\345\220\264\345\230\211\347\216\262/\345\233\276\347\211\207/crue\345\242\236.png" "b/\345\220\264\345\230\211\347\216\262/\345\233\276\347\211\207/crue\345\242\236.png" new file mode 100644 index 0000000000000000000000000000000000000000..c0c7858fad8466378b43c48a7f485a95aec7c051 Binary files /dev/null and "b/\345\220\264\345\230\211\347\216\262/\345\233\276\347\211\207/crue\345\242\236.png" differ