diff --git "a/\351\273\216\346\254\243\346\254\243/20241202--\346\225\260\346\215\256\345\272\223\346\224\271\346\223\215\344\275\234.md" "b/\351\273\216\346\254\243\346\254\243/20241202--\346\225\260\346\215\256\345\272\223\346\224\271\346\223\215\344\275\234.md" new file mode 100644 index 0000000000000000000000000000000000000000..1e7a8ca221097ae069d4932a3cabc8e9c67454f5 --- /dev/null +++ "b/\351\273\216\346\254\243\346\254\243/20241202--\346\225\260\346\215\256\345\272\223\346\224\271\346\223\215\344\275\234.md" @@ -0,0 +1,272 @@ +# 作业 +## 效果图 +![综合练习CRUD](./image/CRUD操作.mp4) +## 主要代码 +```c# +//控制器 +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] + [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) + { + // 根据传入的id,从数据库中拿到最新的值 + var blog = Db.Blogs.FirstOrDefault(x => x.Id == input.Id);// Linq 集成查询(通常都会和Lambda) + + 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 }); + + // 根据传入的要编辑的id,拿到要编辑的博客文章 + + // 通过一定方式,拿到那个博客 + 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) + { + // 根据提供(传入的)id,尝试在对应的数据库表中查找对就的记录,找到则返回那个元素,找不到则返回null + var blog = Db.Blogs.FirstOrDefault(x => x.Id == id); + + // 如果blog为空,则提示要找的那个元素不存在,反之,就显示那个元素及信息,准备删除 + 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"); + } +} + +//视图 +//Index + + +
+ +
+
+
+ + +
+
+
+ 新增 +
+ +
+ + + + + + + + + + + + + @foreach (var blog in @Model) + { + + + + + + + + } + +
Id标题内容作者操作
@blog.Id@blog.Title@blog.Content@blog.Author + 编辑 + 删除 +
+ +
+ +//Create +@model Blog.Models.Blogs; + +
+
+
+
+ +
+//Edit +@model Blog.Models.Blogs; + +
+
+
+
+
+ +
+ + +//Delete +@model Blog.Models.Blogs; + +

你真的要删除以下元素吗?

+ + + + + + + + + + + + + + + + + + +
标题:@Model.Title
内容:@Model.Content
作者:@Model.Author
+ 删除 + @*
+
+ +
*@ +
+ 取消删除 +
+ +//模型 +//Db() +using Microsoft.Extensions.ObjectPool; + +namespace Blog.Models; +public static class Db{ + public static List Blogs{get;set;} + static Db(){ + Blogs=new List(); + for (int i = 0; i <15; i++) + { + var a=i+1; + var tmp=new Blogs{ + + Id=a, + Title="哈哈哈"+a, + Content="共和国是放假回家"+a, + Author="轩辕故里"+a, + }; + + Blogs.Add(tmp); + + } +} +} + +//Blogs +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!; + +} + +``` +# Linq集成查询 +作用在结集合身上,特别是实现了IEnumerable接口的集合上,方法参数中,一般是Lambda表达式 (匿名函数) + +- 查询单个函数 + - .First()函数中可以写查找第一个条件,形如(t=>t.id==id).但是,如果没有第一个元素,或者没有符合条件的第一个元素,则报错 + +- 查询多个函数 + - Where()条件函数,可以查找符合一定条件的元素,反悔的事一个集合 + +- 重新设计返回的数据类型 + - select()这个函数可以帮助我们处理函数返回的真正内容 \ No newline at end of file diff --git "a/\351\273\216\346\254\243\346\254\243/20241204--\345\210\240\351\231\244\343\200\201\346\237\245\346\211\276\346\223\215\344\275\234.md" "b/\351\273\216\346\254\243\346\254\243/20241204--\345\210\240\351\231\244\343\200\201\346\237\245\346\211\276\346\223\215\344\275\234.md" new file mode 100644 index 0000000000000000000000000000000000000000..d83537a681c427c5ac276458158505babe7be7c8 --- /dev/null +++ "b/\351\273\216\346\254\243\346\254\243/20241204--\345\210\240\351\231\244\343\200\201\346\237\245\346\211\276\346\223\215\344\275\234.md" @@ -0,0 +1,112 @@ +# 作业 +## 主要代码 +```c# +//控制器 +using Microsoft.AspNetCore.Mvc; +using Blog.Models; + +namespace Blog.Controllers; +public class BlogsController : Controller +{ + public ActionResult Index() + { + NumberModel model = new NumberModel + { + Numbers = new int[] { 1, 2, 3, 4, 5, 6 } + }; + return View(model); + } + + // POST: Numbers/FindFive + [HttpPost] + public ActionResult FindFive(NumberModel model) + { + if (ModelState.IsValid) + { + // 模拟查找等于5的元素 + var fives = model.Numbers.Where(n => n == 5).ToArray(); + + // 将找到的元素传递给视图 + ViewBag.Fives = fives; + } + + return RedirectToAction("Index"); + + } +} + +//视图 +@model NumberModel + +

数组中的数字

+ + +@using (Html.BeginForm("FindFive", "Numbers", FormMethod.Post)) +{ + +} + +//模型 +namespace Blog.Models; + +public class NumberModel +{ + public int[] Numbers { get; set; }=null!; +} +``` + +# 笔记 +在C# MVC框架中,实现数据库的删除和查找操作的步骤和思路可以分为以下几个部分: +### 1. 定义模型(Model) + +首先,你需要定义一个模型(Model),它代表了数据库中的表结构。这个模型包含了数据库表中字段的映射。 + +### 2. 创建静态数据库和操作 + +由于我们不使用真实的数据库,我们可以创建一个静态的列表来模拟数据库,并提供静态方法来操作这个“数据库”。 + +### 3. 实现控制器(Controller) + +控制器负责处理用户的请求,并调用模型中的方法来执行具体的业务逻辑。 + +### 4. 创建视图(View) + +视图负责显示数据给用户,并提供用户界面来接收用户的输入。 + +### 具体操作步骤和思路: + +#### 步骤1:定义模型 + +创建一个`Person`类,它包含了`Id`、`Name`和`Age`属性,这些属性对应数据库表中的字段。 + +#### 步骤2:创建静态数据库和操作方法 + +创建一个静态类`PersonRepository`,它包含了一个静态列表`People`来存储`Person`对象,以及三个静态方法:`GetAll`、`FindById`和`Delete`。 + +- `GetAll`方法返回所有`Person`对象的列表。 +- `FindById`方法根据`Id`查找特定的`Person`对象。 +- `Delete`方法根据`Id`删除特定的`Person`对象。 + +#### 步骤3:实现控制器 + +创建一个`PersonController`类,它包含了三个动作方法: + +- `Index`方法:调用`PersonRepository.GetAll`方法获取所有人员信息,并返回一个视图显示这些信息。 +- `Delete`方法:接收一个`Id`参数,调用`PersonRepository.Delete`方法删除对应的人员信息,然后重定向到`Index`方法。 +- `Search`方法:接收一个`name`参数,调用`PersonRepository.GetAll`方法获取所有人员信息,并根据`name`参数过滤结果,然后返回一个视图显示过滤后的结果。 + +#### 步骤4:创建视图 + +创建相应的视图文件,用于显示人员列表和提供搜索功能。 + +- `Index`视图:显示所有人员信息,并为每个人员提供一个删除链接。 +- `Search`视图:提供一个搜索表单,允许用户输入姓名进行搜索。 + +#### 步骤5:测试 + +运行应用程序并测试删除和查找功能是否正常工作。 diff --git "a/\351\273\216\346\254\243\346\254\243/image/CRUD\346\223\215\344\275\234.mp4" "b/\351\273\216\346\254\243\346\254\243/image/CRUD\346\223\215\344\275\234.mp4" new file mode 100644 index 0000000000000000000000000000000000000000..00d46a99cba57c3f824fe1891ac1753bffb2a664 Binary files /dev/null and "b/\351\273\216\346\254\243\346\254\243/image/CRUD\346\223\215\344\275\234.mp4" differ