diff --git "a/\350\214\203\344\275\263\346\254\243/20241202\347\274\226\350\276\221\346\223\215\344\275\234\345\222\214\351\233\206\346\210\220\346\237\245\350\257\242.md" "b/\350\214\203\344\275\263\346\254\243/20241202\347\274\226\350\276\221\346\223\215\344\275\234\345\222\214\351\233\206\346\210\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..4572f1dcaf3be48ccc4e77eba1dcba43fa701e9e --- /dev/null +++ "b/\350\214\203\344\275\263\346\254\243/20241202\347\274\226\350\276\221\346\223\215\344\275\234\345\222\214\351\233\206\346\210\220\346\237\245\350\257\242.md" @@ -0,0 +1,69 @@ +# 笔记 +## 列表编辑 + +[HttpPost] +:用于向服务器传输列表,文件上传数据等操作。 +public IActionResult index_4(Student input){ + + var maxId= +} +## linq集成查询 +:作用在谁身上,答案是集合,特别是实现了 +IEnumerable接口的集合上,在方法参数当中,一般是lambta表达式(其实就是匿名函数 +) +-查询单个元素 +--First()函数可以写查找;函数中可以写查找第一个条件,形如(t=>t.Id==id)。但是,如果没有第一个元素,或者没有符合条件第一个元素,则报错 +--FirstOrDefault();类似上面的用法,但是在没有符合条件的时候,不报错,而是返回一个null +-查询多个数据 +--Where();条件函数,可以查找符合一定条件的元素,返回的是一个集合 + +-限制,重新设计返回的数据类型 +--select();这个函数可以帮助我们处理函数返回的真正内容 +```js + + + - 查询单个元素 + - .Frist() + - .FristOrDefault() + - 查询多个元素 + - Where() + - 重新设计返回的数据类型 + - select() + // 可重命名,也可以直接写{ x.Id, x.Author }* + [ValidateAntiForgeryToken] + 是一种用于防止跨站请求伪造(CSRF)攻击的机制。在ASP.NET MVC中,它用于确保提交到服务器的表单数据是由同一用户会话中的合法客户端生成的,而不是由恶意网站生成的。 + + ‌ModelState.IsValid‌ + 是ASP.NET MVC 和 ASP.NET Core 中用于表示模型数据是否有效的布尔属性。当模型的数据验证通过时,ModelState.IsValid 的值为 true;如果数据验证失败,则值为 false。 + if (ModelState.IsValid) { + // 数据有效,执行相应的操作 + } +``` + + +## 代码举例: +```js +[HttpPost] +// HttpPost:是用来向服务器传输一些,表单数据,上传文件或者执行其他提交数据操作 + [ValidateAntiForgeryToken] + public IActionResult Edit(Blue input) + { + if(ModelState.IsValid){ + var black=Db.Blue.FirstOrDefault(x=>x.Id==input.Id); + if(black!=null){ + black.Title=input.Title; + black.Author=input.Author; + black.Content=input.Content; + } + return View(input); + } + return View(); + } + public IActionResult Edit(int id) + { + var list=Db.Blue.Where(x=>x.Id==id&&x.Author=="小小"); + var list1=Db.Blue.Select(x=>new{Xyz=x.Id,Abc=x.Author}); + var black=Db.Blue.FirstOrDefault(x=>x.Id==id); + return View(black); + } +``` \ No newline at end of file diff --git "a/\350\214\203\344\275\263\346\254\243/20241204\345\210\240\351\231\244\345\222\214\346\237\245\346\211\276\346\223\215\344\275\234.md" "b/\350\214\203\344\275\263\346\254\243/20241204\345\210\240\351\231\244\345\222\214\346\237\245\346\211\276\346\223\215\344\275\234.md" new file mode 100644 index 0000000000000000000000000000000000000000..28f4dcc87760a2943d1bbb70812151677cd77198 --- /dev/null +++ "b/\350\214\203\344\275\263\346\254\243/20241204\345\210\240\351\231\244\345\222\214\346\237\245\346\211\276\346\223\215\344\275\234.md" @@ -0,0 +1,497 @@ +# 笔记 +## 删除操作 +对应删除页面的试图(在控制器当中进行操作) + 1:根据传入的id,尝试在对应的数据库表中查找对应的记录,找到则返回那个元素,找不到则返回null + 2: 如果blog为空,则提示要找到的那个元素不存在,反之 就显示那个元素及信息,准备删除 +2. 对应删除操作 + 1) 根据传入的id,尝试在对应的数据库表中查找对应的记录 + 2) 删除操作:`Remove()` +### 1.***首先创建一个student.cs的模型*** +```cs +// 首先创建一个Student.cs的模型 +namespace Student.Models; +public class Student{ + public int id{get;set;} + public string name{get;set;}=null!; + public string sex{get;set;}="m"; + // 定义一个默认的性别属性 + public string address{get;set;} + +} +``` +### way1:使用post表单形式 +```js +public IActionResult Delete(int id) +{ + var cont = Db.Shuju.FirstOrDefault(x=>x.Id==id); + if (cont!=null){ + return View(cont); + }; + return View(); +} + + // 控制的是删除的操作 + [HttpPost] + public IActionResult DeleteCont(int id) + { + // 删除方法1:post 使用表单形式 + // 这个将页面内容删除 使用post表单删除的方法 + + var cont = Db.Shuju.FirstOrDefault(x=>x.Id==id); + // 如果不为空则删除 + if (cont!=null) + { + Db.Shuju.Remove(cont); + }; + // 删除后返回首页 + returnRedirectToAction("Index"); + } + ``` + + +在view视图当中: +1:展现出需要删除的元素 +2:确定删除:对应代码元素,一定要获取到对应的内容 +3:不需要的话,则返回首页 +# 查找操作 +1. 在首页视图中添加查找框(以表单的形式),**在查找框内增加`name = Keyword`属性**,跟查找内容传输相连接 +**在视图中**: +
+
+ +
+ + +``` +2. 在首页的控制器中填入查询代码: + - 填写string参数,接收查找框传入的数据 + - 判断数据是否成功传入 + - 没有传入,则直接显示首页 + - 传入了就进行内容匹配,显示匹配结果 +`` +### way2:get a标签 +```js + + +**在控制器中**: + // 控制的是删除的页面 + public IActionResult Delete(int id) + { + // 将内容显示到视图(这是一个删除的页面 显示删除还是不删的页面) + + // 根据提供(传入的)id,尝试在对应的数据库表中查找对应的记录,找到则返回那个元素,找不到则返回null + var cont = Db.Shuju.FirstOrDefault(x=>x.Id==id); + + // 如果cont为null,则提示要找的那个元素不存在,反之,就显示那个元素及信息,准备删除 + + if (cont!=null) + { + return View(cont); + }; + return View(); + } + // 控制的是删除的操作 + public IActionResult DeleteOther(int id) + { + // 删除方法2:get 不使用表单形式,使用a标签对应的id进行删除 + + var cont = Db.Shuju.FirstOrDefault(x=>x.Id==id); + // 如果不为空则删除 + if (cont!=null) + { + Db.Shuju.Remove(cont); + }; + // 删除后返回首页 + return RedirectToAction("Index"); + } + + +**在视图中**: + // 使用a标签 根据id来操作 + 删除 + +``` + +# 作业 + +>代码展示: +```js +public class LinqController:Controller +{ + public IActionResult One() + int[] num={1,2,3,4,5,6}; + var a=num.Where(n => n == 5).ToList(); + return View(a); +} +``` + 连 +>代码展示 +```js +public IActionResult Two(){ +int[] num={1,2,3,4,5,6}; +var a1=num.Where(n => n >=2 && n<=8); +return View(a1) +} +``` + + >代码展示 +```js +public IActionResult Three(){ +int[] num={1,2,3,4,5,6}; +var a2=num.Where(n => n*2); +return View(a1) +} +``` + + >代码展示 + ```js +public IActionResult Four() + { + + List students = new List + { + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "王中王", Age = 22 }, + new Student {Id=3, Name = "张语嫣", Age = 23 }, + new Student {Id=4, Name = "詹宇航", Age = 35 }, + new Student {Id=5, Name = "郑雨良", Age = 26 }, + }; + var a3= students.Where(n => n.Name.StartsWith("王")) + return View(a) + } + ``` + +>代码展示 + ```js + public IActionResult Five() + { + + List students = new List +{ + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, +}; + var a4 = students.Where(n => n.Age > 20).OrderByDescending(n => n.Age); + return View(a4); + } +``` + + >代码展示 +```js + public IActionResult Six() + { + + int[] num = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var o = num.GroupBy(n => n).ToDictionary(n => n.key, n.Count()); + var a5 = numbers.Where(n => o[n] != 2); + return View(a5); + } +``` + + >代码展示 +```js + public IActionResult Seven() + { + + int[] num = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a6 = num.Where(n => n > 3).First(); + return View(a6); + } +``` + + >代码展示 + ```js + public IActionResult Eight() + { + + int[] num = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a7= num.LastOrDefault(n => n < 7); + return View(a7) + } +``` + + >代码展示 +```js + public IActionResult Nine() + { + int[] num = { 1, 2, 3, 4, 5, 6, 14, 23, 64, 7, 18, 2, 3 }; + bool a8 = Array.Exists(num, a8 => num > 10); + viewBag.nums = a8; + return View(a8) + } +``` + + >代码展示 +```js + public IActionResult Ten() + { + int[] num = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a9 = num.Count(n => n > 5); + return View(a9) + } +``` + + >代码展示 +```js +public IActionResult Eleven() + { + + int[] num = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a10 = num.sum(n => n); + return View(a10) + } +``` + + >代码展示 +```js + public IActionResult Twelve() + { + + int[] num= { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a11 = num.Max(n => n); + return View(a11) + } +``` + + >代码展示 +```js + public IActionResult Thirteen() + { + + int[] num = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a12 = num.Min(n => n); + return View(a12) + } +``` + + >代码展示 +```js + public IActionResult Fourteen() + { + + int[] num = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a13 = num.Average(n => n); + return View(a13) + } +``` + +>代码展示 +```js + public IActionResult Fifteen() + { + + int[] num = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a14 = num.Where(n => n % 3 == 0); + return View(a14) + } +``` + ### 中级练习 + +>代码展示 +```js + public IActionResult Sixteen() + { + List students = new List +{ + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, +}; + var a15 = students.Select(n => new { n.Name, n.Age }).ToList(); + return View(a15) + } +``` + +>代码展示 +```js + public IActionResult Seventeen() + { + List students = new List +{ + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, +}; + var a16 = students.GroupBy(n => n.Age ).Select(n=>new{Age=n.key,Count=n.Count()}); + return View(a16) + } +``` + + >代码展示 +```js + public IActionResult Eighteen() + { + + List students = new List +{ + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, +}; + List courses = new List +{ + new Course{StudentId=1,CourseName="英语"}, + new Course{StudentId=1,CourseName="数学"}, + new Course{StudentId=2,CourseName="语文"}, + new Course{StudentId=3,CourseName="物理"}, + new Course{StudentId=4,CourseName="化学"}, + new Course{StudentId=4,CourseName="生物"}, + new Course{StudentId=4,CourseName="语文"}, +}; +var a17=students.Select(students=>new{ + studentId=students.Id, + StudentName=students.Name, + Courses=courses.Where(courses=>courses.StudentId==students.Id).Select(course=>course.CourseName).ToList() + +}).ToList(); +return View(a17) + } +``` + + >代码展示 +```js + + public IActionResult Nineteen() + { + + + int[] num = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a18=num.Reverse(); + return View(a18) + } +``` + + >代码展示 +```js + public IActionResult Twenty() + { + int[] num = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a21=num.Where(n=>n>2).Selete(n=>2); + return View(a21) + } +``` + + >代码展示 +```js + public IActionResult Twenty_One() + { + int[] num = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a22=num.Where(n=>n>5); + return View(a22) + } +``` + + >代码展示 +```js + public IActionResult Twenty_Two() + { + + + int?[] nullableNumbers = { 1, null, 3, null, 5 }; + var a23=nullableNumbers.Selete(n=>n??0); + return View(a23) + } +``` + + + >代码展示 +```js + public IActionResult Twenty_Three() + { + string[] stringNumbers = { "1", "2", "3", "4" }; + int a24=stringNumbers.Selete(n=>int.Parse(n)).ToArray(); + return View(a24) + } + ``` + + >代码展示 +```js + public IActionResult Twenty_Four() + { + + + object[] objects = { "String", 123, "Another String", 456 }; + var a25 = objects.OfType().ToList(); + return View(a25) + } +``` + + >代码展示 +```js + public IActionResult Twenty_Five() + { + + + int[] num1 = { 1, 2, 3 }; + int[] num2 = { 4, 5, 6 }; + return View() + } +``` + + >代码展示 +```js + public IActionResult Twenty_Six() + { + + int[] a27= Enumerable.Range(1, 10).ToArray(); + + return View(a27); + } +``` + + >代码展示 +```js + public IActionResult Twenty_Seven() + { + + + int[] a28 = Enumerable.Repeat(7, 5).ToArray(); //77777 + + return View(a28); + } +``` + + >代码展示 + ```js + public IActionResult Twenty_Eight() + { + + int[] num = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var a29 = num.Take(5); + return View(a29); + } + ``` + + >代码展示 +```js + public IActionResult Twenty_Nine() + { + + + int[] num = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var a30 = num.Skip(3); + return View(a30); + } + +```