diff --git "a/\346\254\247\345\272\255\347\200\232/\347\254\224\350\256\260/20241202-\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\346\254\247\345\272\255\347\200\232/\347\254\224\350\256\260/20241202-\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..969b47690103f8f47d126f01a90ff4d44acf5225 --- /dev/null +++ "b/\346\254\247\345\272\255\347\200\232/\347\254\224\350\256\260/20241202-\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,79 @@ +## 概念 +1. 模型(Model): + - 增加(Create):模型负责创建新的数据对象。例如,在一个博客系统中,模型可能会创建一个新的文章对象,并将其保存到数据库中。 + - 读取(Read):模型从数据库中检索数据。例如,它可能会获取所有文章的列表或特定文章的详细信息。 + - 更新(Update):模型更新现有数据对象。例如,它可能会更新文章的内容或标题,并保存这些更改到数据库中。 + - 删除(Delete):模型从数据库中删除数据对象。例如,它可能会删除一个不再需要的文章。 +2. 视图(View):视图是用户界面,负责显示数据(模型)和接收用户输入。在增删改查操作中,视图会显示表单供用户输入数据,显示数据列表,以及显示操作的结果(成功或失败的消息)。 +3. 控制器(Controller):控制器是应用程序的中介,它接收用户的输入并调用模型和视图去完成用户的需求。 + - 增加(Create):控制器接收用户提交的表单数据,调用模型创建新数据对象,然后可能重定向到一个视图以显示确认消息或新创建的对象。 + - 读取(Read):控制器请求模型提供数据,然后选择一个视图来展示这些数据。 + - 更新(Update):控制器接收用户对数据的更改,调用模型更新数据对象,然后可能重定向到一个视图以显示更新后的对象或确认消息。 + - 删除(Delete):控制器处理删除请求,调用模型删除数据对象,然后可能重定向到一个视图以显示确认消息或更新后的数据列表。 +## 步骤 +1. 创建一个mvc项目`dotnet new mvc -o Blog` + +2. 创建一个控制器--BlogsController.cs + ``` + using Microsoft.AspNetCore.Mvc; + namespace Blog.Controller; + public class BlogsController:Controller + { + public IActionResult Index() + { + return View(); + } + } + ``` + +3. 创建对应的视图Blogs--Index.cshtml + - 可以在里面使用`xx`来跳转页面 + - 引用css link `href="~/css/base.css"` + - 声明`@model List`--可以提示 + +4. 创建模型 Blogs.cs + ``` + namespace Blog.Models; + public class Blogs + { + public int Id{get;set;} + public string Name{get;set}=null!; //不为空 + + } + ``` + +5. 创建一个模型Db.cs(模拟数据库) 在里面放集合数据 + ``` + namespace Blog.Models; + public static class Db + { + public static Listblogs{get;set;} + //构造函数 + static Db + { + blogs=[]; + for(var i=1;i<10;i++) + { + var t=new Blogs + { + Id=i, + Name="哈哈" + }; + blogs.Add(t); //添加到blogs中 + } + } + } + ``` + +6. 需要在BlogsController控制器中的Index函数中`return View(Db.blogs);`,在控制器中也要引用`using Blog.Models;` + +7. 在视图中显示集合数据 + ``` + @foreach(var item in @Model) + { + + @item.Id + @item.Name + + } + ``` \ No newline at end of file diff --git "a/\346\254\247\345\272\255\347\200\232/\347\254\224\350\256\260/20241204-Linq\351\233\206\346\210\220\346\237\245\350\257\242\357\274\210\345\205\263\350\201\224Lambda\357\274\211.md" "b/\346\254\247\345\272\255\347\200\232/\347\254\224\350\256\260/20241204-Linq\351\233\206\346\210\220\346\237\245\350\257\242\357\274\210\345\205\263\350\201\224Lambda\357\274\211.md" new file mode 100644 index 0000000000000000000000000000000000000000..45e95d3dbe232699a22a1db47b3b8d01c9c033de --- /dev/null +++ "b/\346\254\247\345\272\255\347\200\232/\347\254\224\350\256\260/20241204-Linq\351\233\206\346\210\220\346\237\245\350\257\242\357\274\210\345\205\263\350\201\224Lambda\357\274\211.md" @@ -0,0 +1,21 @@ +## Linq集成查询 ( 关联Lambda ) + +1. First FirstOrDefaualt 找第一个符合条件的元素 + + - First(x=>x.Id==id) 返回第一个Id等于id的元素,如果都没有符合的,报错 + - FirstOrDefaualt(x=>x.Id==id) 返回第一个Id等于id的元素,如果都没有符合的,返回Null + +2. Single SingleOrDefault + + - 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; \ No newline at end of file diff --git "a/\346\254\247\345\272\255\347\200\232/\347\254\224\350\256\260/20241205-\345\210\240\351\231\244.md" "b/\346\254\247\345\272\255\347\200\232/\347\254\224\350\256\260/20241205-\345\210\240\351\231\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..201976786f3c2a46a008b7a0a38afe5700893bbe --- /dev/null +++ "b/\346\254\247\345\272\255\347\200\232/\347\254\224\350\256\260/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