From 847b52edb8798dc0b4c3172b210b727d985b8f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=AE=87=E5=A7=97?= <14091760+tangyushan123@user.noreply.gitee.com> Date: Sun, 8 Dec 2024 18:52:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\346\237\245\351\241\265\351\235\242.md" | 55 +++ ...26\350\276\221\346\225\210\346\236\234.md" | 86 ++++ ...40\351\231\244\346\237\245\346\211\276.md" | 428 ++++++++++++++++++ 3 files changed, 569 insertions(+) create mode 100644 "\345\224\220\345\256\207\345\247\227/20241202-\345\242\236\345\210\240\346\224\271\346\237\245\351\241\265\351\235\242.md" create mode 100644 "\345\224\220\345\256\207\345\247\227/20241204-mvc\345\242\236\345\212\240\357\274\214\347\274\226\350\276\221\346\225\210\346\236\234.md" create mode 100644 "\345\224\220\345\256\207\345\247\227/20241205-mvc\345\210\240\351\231\244\346\237\245\346\211\276.md" diff --git "a/\345\224\220\345\256\207\345\247\227/20241202-\345\242\236\345\210\240\346\224\271\346\237\245\351\241\265\351\235\242.md" "b/\345\224\220\345\256\207\345\247\227/20241202-\345\242\236\345\210\240\346\224\271\346\237\245\351\241\265\351\235\242.md" new file mode 100644 index 0000000..b077022 --- /dev/null +++ "b/\345\224\220\345\256\207\345\247\227/20241202-\345\242\236\345\210\240\346\224\271\346\237\245\351\241\265\351\235\242.md" @@ -0,0 +1,55 @@ +# 步骤 +1. dotnet new mvc -o Blog (新建一个Blog文件) + +2. BlogsController(新建控制器) + - namespace Blog.Controllers;(控制器里的命名空间) + - using Microsoft.AspNetCore.Mvc;(最好记住) + - public class BlogsController:Controller{ + + public IActionResult Index(){ + return Views(); + } + } +3. 再创建对应的视图,文件夹名对应控制器名,视图下的cshtml文件名字对应控制器内部函数名字。 + - 在index.cshtml中写增删改查页面内容 +4. 在wwwroot文件夹下的css文件夹下建一个base.css文件link到index.cshtml中,可以写样式 + - `` +5. 在Model里增加Blogs.cs/Db.cs + - namespace Blog.Models;(这位model里的命名空间) + ``` + 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!; + + } + ``` + + - Db中 + + ``` + namespace Blog.Models; + + public static class Db + { + public static List blogs {get;set;} + static Db() + { + blogs=[]; + + for(int i =0 ;i<10;i++){ + var tmp = new Blogs + { + ID=i, + Title=$"永远是朋友{i}", + Content=$"假日风情{i}", + Author=$"仙仙{i}" + }; + blogs.Add(tmp) + } + + } + } + ``` \ No newline at end of file diff --git "a/\345\224\220\345\256\207\345\247\227/20241204-mvc\345\242\236\345\212\240\357\274\214\347\274\226\350\276\221\346\225\210\346\236\234.md" "b/\345\224\220\345\256\207\345\247\227/20241204-mvc\345\242\236\345\212\240\357\274\214\347\274\226\350\276\221\346\225\210\346\236\234.md" new file mode 100644 index 0000000..56803c4 --- /dev/null +++ "b/\345\224\220\345\256\207\345\247\227/20241204-mvc\345\242\236\345\212\240\357\274\214\347\274\226\350\276\221\346\225\210\346\236\234.md" @@ -0,0 +1,86 @@ +# 新增页面 +*表单* +```csharp +@model Blog.Models.Blogs; + +
+
+
+
+ +
+``` +```csharp + + 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. 已经验证表单数据可以传入。 + 2. 拿到传入数据后,一般做数据验证(用户名是否存在,必填,手机号格式,长度是否符合长度。。。)。 + 3. 若符合验证规则,则保存到数据库里否则提示验证不通过。 + 4. 若保存数据库成功。则跳转到表面,不通过,那仍然显示新增页面。 + } + + // 先通过select 拿到集合中的所有id,放在一个新的集合中返回,然后对这个返回的新的集合应用Max方法,找到其中最大值 + // var blogs = Db.Blogs.Where(x => x.Title.Equals(input.Title)); + // if (blogs.Count() > 0) + // { + // return View("create"); + // } +``` +# 编辑页面 +```csharp +@model Blog.Models.Blogs; + +
+
+
+
+
+ +
+``` +```csharp + public IActionResult Edit(int id) + { + // 根据id找到对应的blogs,有可能为空 + var blog = Db.Blogs.FirstOrDefault(x => x.Id == id); + return View(blog); + } + + [HttpPost] + public IActionResult Edit(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; + + } + return RedirectToAction("Index"); + } +``` + +# 三、Linq集成查询(关联Lambda) +First FirstOrDefaualt 找第一个符合条件的元素 + +First(x=>x.Id==id) 返回第一个Id等于id的元素,如果都没有符合的,报错 +FirstOrDefault(x=>x.Id==id) 返回第一个Id等于id的元素,如果都没有符合的,返回Null +Single SingleOrDefault + +Single() 返回第一个元素,如果没有,报错 +SingleOrDefault() 返回第一个元素,如果没有,返回Null +Where + +Where(x=>x.Score>=80 && x.Sex==1) 查找所有成绩大于等于80,并且性别为1的所有元素 +Select + +Select(x=>new {x.Id,x.Score}) 以新的{x.Id,x.Score}对象的形式,返回新的集合 +from XX in XX where XXX select; \ No newline at end of file diff --git "a/\345\224\220\345\256\207\345\247\227/20241205-mvc\345\210\240\351\231\244\346\237\245\346\211\276.md" "b/\345\224\220\345\256\207\345\247\227/20241205-mvc\345\210\240\351\231\244\346\237\245\346\211\276.md" new file mode 100644 index 0000000..a877323 --- /dev/null +++ "b/\345\224\220\345\256\207\345\247\227/20241205-mvc\345\210\240\351\231\244\346\237\245\346\211\276.md" @@ -0,0 +1,428 @@ + +# 删除 +```csharp +@model Blog.Models.Blogs; + +

你要删除以下内容吗?

+ + + + + + + + + + + + + + + + + + +
标题:@Model.Title
内容:@Model.Content
作者:@Model.Author
+ + @* *@ + + + @* *@ + + +
+``` +```csharp + /// + /// 这个action用于展示删除确认页面 + /// + /// + /// + + public IActionResult Delete(int id){ + var blog = Db.Blogs.FirstOrDefault(x=>x.Id==id); + return View(blog); + } + + public IActionResult DeleteConfirm(int id){ + var blog =Db.Blogs.FirstOrDefault(x=>x.Id==id); + if(blog!=null){ + Db.Blogs.Remove(blog); + return RedirectToAction("Index"); + } + return NotFound(); + } +``` +# 查找 +```csharp + public IActionResult Index(string 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); + } +``` +# 初级 +```csharp +using System.Linq; +using Microsoft.AspNetCore.Mvc; +using Vlog.Models; +namespace Vlog.Controllers; +public class OneController : Controller +{ +public IActionResult One() +{ + //查询特定元素 找出数组中等于5的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var first = numbers.Where(n => n==5).ToList(); + return View(first); + +} +public IActionResult Two(){ + //查询特定范围的元素 找出数组中在2到8之间的元素 + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var second = numbers.Where(n => n>2 && n<8).ToList(); + + return View(second); + +} +public IActionResult Three(){ + //查询并转换元素 将数组中的每个数字乘以2。 + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var third = numbers.Select(n =>n*2).ToList(); + + return View(third); + +} +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 four = students.Where(n => n.Name.Contains("王")).ToList(); + return View(four); + +} +public IActionResult Five(){ + //查询并排序 找出所有年龄大于20岁的学生,并按年龄降序排列。 + + 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 five = students.Where(n => n.Age>20).OrderByDescending(s => s.Age).ToList(); + return View(five); + +} +public IActionResult Six(){ + //查询并去重 找出数组中所有不重复的数字 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var six = numbers.Distinct().ToList(); + return View(six); +// Distinct() 方法的工作原理如下: + +// 1.它遍历整个集合。 +// 2.它检查每个元素是否已经存在于结果集中。 +// 3.如果元素不存在,则将其添加到结果集中。 +// 4.如果元素已经存在,则忽略它,不添加到结果集中。 +} +public IActionResult Seven(){ + //查询第一个元素 找出数组中第一个大于3的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var seven = numbers.FirstOrDefault(n =>n>3); + + return View(seven); + +} +public IActionResult Eight(){ + //查询最后一个元素 找出数组中最后一个小于7的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var eight = numbers.LastOrDefault(n =>n<7); + + return View(eight); + +} +public IActionResult Nine(){ + //查询元素是否存在 检查数组中是否存在大于10的元素 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + bool nine = numbers.Any(n => n > 10); + //Any() 方法用于检查数组 numbers 中是否存在任何元素满足条件 + return View(nine); + +} +public IActionResult Ten(){ + //查询元素的计数 计算数组中大于5的元素数量 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + int ten = numbers.Count(n => n > 5); + + return View(ten); + +} +public IActionResult Eleven(){ + //查询元素的总和 计算数组中所有元素的总和 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + int eleven = numbers.Sum(); + + return View(eleven); + +} +public IActionResult Onetwo(){ + //查询元素的最大值 找出数组中的最大值 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + int onetwo = numbers.Max(); + + return View(onetwo); + +} +public IActionResult Onethree(){ + //查询元素的最小值 找出数组中的最小值 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + int onethree = numbers.Min(); + + return View(onethree); + +} +public IActionResult Onefour(){ + //查询元素的平均值 找出数组中的平均值 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + double onefour = numbers.Average(); + + return View(onefour); + +} +public IActionResult Onefive(){ + //查询特定条件的元素 找出数组中能被3整除的元素 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var onefive = numbers.Where(n=> n % 3 ==0).ToList(); + + return View(onefive); + +} +} +``` +# 中级 +```csharp +using Microsoft.AspNetCore.Mvc; +namespace Vlog.Controllers; +using Vlog.Models; +using System.Linq; + public class TwoController:Controller{ + public IActionResult Index(){ + return View(); + } + + public IActionResult Ones(){ + //16.查询并选择特定属性 找出所有学生的姓名和年龄。 + 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 ones = students.Select(n=>new {n.Name , n.Age}).ToList(); + return View(ones); + } + public IActionResult Two(){ + //17.查询并分组 按年龄分组学生,并计算每个年龄组的学生数量。 + 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 two= students.GroupBy(s=>s.Age).Select(g => new { Age = g.Key, Count = g.Count() }).ToList(); + return View(two); + } + public IActionResult Thee(){ + //18.查询并联结 联结学生和课程表,找出每个学生的所有课程。 + 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 three = from student in students + join course in courses on student.Id equals course.StudentId + into studentCourses + select new + { + StudentName = student.Name, + Courses = studentCourses.Select(c => c.CourseName).ToList() + }; + + return View(three.ToList()); + + } + public IActionResult Four(){ + // 19.查询并反转 反转数组中的元素顺序 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var reversedNumbers = numbers.AsEnumerable().Reverse(); + + // 将结果转换回数组 + int[] four = reversedNumbers.ToArray(); + + // 可以将reversedArray传递给视图,或者直接在视图中反转 + return View(four); + + } + public IActionResult Five(){ + //20.查询并排除 从数组中排除所有小于5的元素 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + // 使用LINQ排除所有小于5的元素 + var five = numbers.Where(n => n >= 5).ToList(); + + // 将结果传递给视图 + return View(five); + + } + public IActionResult Six(){ + //21.查询并填充默认值 如果数组中存在null值,用默认值0替换 + int?[] nullableNumbers = { 1, null, 3, null, 5 }; + + // 使用LINQ替换null值为0 + var six = nullableNumbers.Select(n => n ?? 0).ToList(); + + return View(six); + + } + public IActionResult Seven(){ + //22.查询并转换类型 将字符串数组转换为整数数组 + string[] stringNumbers = { "1", "2", "3", "4" }; + // 使用LINQ将字符串数组转换为整数数组 + int[] seven = stringNumbers.Select(int.Parse).ToArray(); + + + return View(seven); + } + public IActionResult Eight(){ + //23.查询并转换类型 将整数数组转换为字符串数组 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + + // 使用LINQ将整数数组转换为字符串数组 + var eight = numbers.Select(n => n.ToString()).ToArray(); + return View(eight); + } + public IActionResult Nine(){ + //24.查询并使用OfType过滤 从对象数组中过滤出字符串类型的元素 + object[] objects = { "String", 123, "Another String", 456 }; + var nine = objects.OfType().ToList(); + + + return View(nine); + } + } + + +``` +# 高级 +```csharp +using Microsoft.AspNetCore.Mvc; +namespace Vlog.Controllers; +using System.Collections.Generic; +using System.Linq; + public class ThreeController:Controller{ + public IActionResult Index(){ + return View(); + } + public IActionResult One(){ + //25.查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组。 + int[] numbers1 = { 1, 2, 3 }; + int[] numbers2 = { 4, 5, 6 }; + + // 使用LINQ的Zip方法合并两个数组 + var one = numbers1.Zip(numbers2, (first, second) => new { first, second }).ToList(); + + + return View(one); + + } + public IActionResult Two(){ + //26.查询并使用Range生成 生成一个包含1到10的整数数组 + // 使用LINQ的Range方法生成1到10的整数数组 + int[] two = Enumerable.Range(1, 10).ToArray(); + + // 将结果传递给视图 + return View(two); + + } + public IActionResult Three(){ + //27.查询并使用Repeat重复 重复一个元素多次,创建一个新数组。 + + + // 要重复的元素 + int number = 3; + // 重复的次数 + int repeatCount = 10; + + // 使用LINQ的Repeat方法重复元素 + var three = Enumerable.Repeat(number, repeatCount).ToArray(); + + // 将结果传递给视图 + return View(three); + + } + public IActionResult Four(){ + //28.查询并使用Take限制数量 从数组中取出前5个元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + + // 使用LINQ的Take方法取出前5个元素 + var four = numbers.Take(5).ToArray(); + + // 将结果传递给视图 + return View(four); + } + public IActionResult Five(){ + //29.查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + + // 使用LINQ的Skip方法跳过前3个元素 + var five = numbers.Skip(3).ToArray(); + + // 将结果传递给视图 + return View(five); + + } + } +``` \ No newline at end of file -- Gitee