From 5646b15b91011fa7ce11a267a6abd1723540c218 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 8 Dec 2024 18:15:24 +0800 Subject: [PATCH] =?UTF-8?q?'MVC=E7=9A=84=E5=A2=9E=E5=88=A0=E6=94=B9?= =?UTF-8?q?=E6=9F=A5=E4=B8=8E=E9=9B=86=E6=88=90=E6=9F=A5=E8=AF=A2'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\346\210\220\346\237\245\350\257\242.md" | 383 ++++++++++++++++++ 1 file changed, 383 insertions(+) create mode 100644 "\351\231\206\346\245\232\347\233\210/20241208\345\242\236\345\210\240\346\224\271\346\237\245\345\222\214\351\233\206\346\210\220\346\237\245\350\257\242.md" diff --git "a/\351\231\206\346\245\232\347\233\210/20241208\345\242\236\345\210\240\346\224\271\346\237\245\345\222\214\351\233\206\346\210\220\346\237\245\350\257\242.md" "b/\351\231\206\346\245\232\347\233\210/20241208\345\242\236\345\210\240\346\224\271\346\237\245\345\222\214\351\233\206\346\210\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..a4c5c6a --- /dev/null +++ "b/\351\231\206\346\245\232\347\233\210/20241208\345\242\236\345\210\240\346\224\271\346\237\245\345\222\214\351\233\206\346\210\220\346\237\245\350\257\242.md" @@ -0,0 +1,383 @@ +# 增删改查 +## 控制器 +``` +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using Blog.Models; + +namespace Blog.Controllers; + +public class BlogsController : Controller +{ + public IActionResult Index(string keyword)//首页 + { + if (string.IsNullOrEmpty(keyword)) + { + return View(Db.Blogs); + } + else + { + var list = Db.Blogs.Where(x => x.Title.Contains(keyword) + || x.Content.Contains(keyword) + || x.Author.Contains(keyword)) + .ToList(); + return View(list); + } + } + + + public IActionResult Create()//添加页 + { + return View(); + } + + + + [HttpPost]// 接受post请求 + [ValidateAntiForgeryToken]//数据验证 + public IActionResult Create(Blogs input) + { + if (ModelState.IsValid) + { + var maxId = Db.Blogs.Select(t => t.ID).Max(); + input.ID = maxId + 1; + Db.Blogs.Add(input); + + return RedirectToAction("Index"); + } + return View(input); + } + [HttpPost] + [ValidateAntiForgeryToken]//数据验证 + public IActionResult Edit(Blogs input) + { + + if(ModelState.IsValid) + { + var blog= Db.Blogs.FirstOrDefault(x => x.ID == input.ID); + if(blog != null) + { + blog.Title=input.Title; + blog.Author=input.Author; + blog.Content=input.Content; + } + return RedirectToAction("Index"); + } + return View(input); + } + public IActionResult Edit(int id) + { + var list=Db.Blogs.Where(x => x.ID ==id && x.Author == "作者"); + var list2 = Db.Blogs.Select(x => new { Xyz = x.ID, Abc = x.Author }); + + var blog=Db.Blogs.FirstOrDefault(x=>x.ID==id); + //返回给视图 + return View(blog); + } + public dynamic Test() + { + var blog = Db.Blogs.FirstOrDefault(x => x.ID == 2); + + var list = Db.Blogs.Where(x => x.ID == 2); + var list2 = Db.Blogs.Select(x => new { x.ID, x.Author }); + return list2; + } + public IActionResult Delete(int id) + { + var blog=Db.Blogs.FirstOrDefault(x=>x.ID==id); + if(blog!=null) + { + return View(blog); + } + return View(); + } + [HttpPost] + public IActionResult DeleteConfirm(int id) + { + var blog = Db.Blogs.FirstOrDefault(x => x.ID == id); + if (blog != null) + { + Db.Blogs.Remove(blog); + } + return RedirectToAction("Index"); + } + public IActionResult DeleteOther(int id) + { + var blog = Db.Blogs.FirstOrDefault(x => x.ID == id); + if (blog != null) + { + Db.Blogs.Remove(blog); + } + return RedirectToAction("Index"); + } +} +``` +## 模型 +``` +Blog.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!; + } +``` +``` +DB.cs + namespace Blog.Models; + + // 模拟数据库(此处是一静态类型) + public static class Db + { + // 静态属性,模拟数据库表 + public static List Blogs { get; set; } + // 静态构造函数,只执行一次 + static Db() + { + // 初始化静态属性 + Blogs = []; + // 给这个集合属性,塞进去一些Blogs + for (int i = 0; i < 5; i++) + { + var id = i + 1; + var tmp = new Blogs + { + ID = id, + Title = $"标题{id}", + Content = $"内容{id}", + Author = $"作者{id}" + }; + + Blogs.Add(tmp); + } + } + } +``` +``` +SearchBlog.cs + namespace Blog.Models; + public class SearchBlog + { + public string?keyword{get;set;} + } +``` +## 视图 +``` +Create.cshtml + @model Blog.Models.Blogs; + +
+
+
+
+ +
+ +``` +``` +@model Blog.Models.Blogs; + +

是否删除该信息?

+ + + + + + + + + + + + + + + + + + +
标题:@Model.Title
内容:@Model.Content
作者:@Model.Author
+ 确认 + + 取消 +
+``` +``` +@* 标题: +
+内容: +
+作者: +
+ *@ + + @model Blog.Models.Blogs; + +
+
+
+
+
+ +
+ + +``` +``` +@model List; + + +
+ + +``` +# 基础 +``` +查询特定元素 找出数组中等于5的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var number1 = numbers.Where(n=>n==5).ToList(); + return View(number1); +``` +``` +查询特定范围的元素 找出数组中在2到8之间的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var num2 = numbers.Where(n => n>=2&&n<=8 ); + return View(num2); +``` +``` +查询并转换元素 将数组中的每个数字乘以2。 + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var num3 = numbers.Select(n => n*2 ); + return View(num3); +``` +``` +查询特定属性的对象 找出所有名字以"王"开头的学生。 + 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 Name = students.Where(n=>n.Name.StartsWith("王")); + return View(Name); +``` +``` +查询并排序 找出所有年龄大于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 Agee = students.Where(n=>n.Age>20).OrderByDescending(n=>n.Age); + return View(Agee); +``` +``` +查询并去重 找出数组中所有不重复的数字。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var Once= numbers.GroupBy(n=>n).ToDictionary(n=>n.Key,n=>n.Count()); + var Onces= numbers.Where(n=>shu[n]!=2); + return View(Onces); +``` +``` +查询第一个元素 找出数组中第一个大于3的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num5 = numbers.Where(n=>n>3).First(); + return View(num5); +``` +``` +查询最后一个元素 找出数组中最后一个小于7的元素 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var last = numbers.LastOrDefault(n=>n<7); + return View(last); +``` +``` +查询元素是否存在 检查数组中是否存在大于10的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + bool ten = Array.Exists(numbers,ten=>ten>10); + ViewBag.nums=ten; + return View(); +``` +``` +查询元素的计数 计算数组中大于5的元素数量。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num6 = numbers.Count(n=>n>5); + return View(num6); +``` +``` +查询元素的总和 计算数组中所有元素的总和。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var toatl = numbers.Sum(n=>n); + return View(toatl); +``` +``` +查询元素的最大值 找出数组中的最大值。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var max = numbers.Max(n=>n); + return View(max); +``` +``` +查询元素的最小值 找出数组中的最小值。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var minn = numbers.Min(n=>n); + return View(minn); +``` +``` +查询元素的平均值 计算数组中所有元素的平均值。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var avg = numbers.Average(n=>n); + return View(avg); +``` +``` +查询特定条件的元素 找出数组中能被3整除的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num = numbers.Where(n=>n%3==0); + return View(num); +``` \ No newline at end of file -- Gitee