From 59016fa918e81fd29fab8f0c9806daf09e382c04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=B6=B5?= <3234558314@qq.com> Date: Sun, 22 Dec 2024 22:15:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\346\225\260\346\215\256\345\272\223.md" | 0 ...14\346\225\264\344\273\243\347\240\201.md" | 201 ++++++++++++++++++ 2 files changed, 201 insertions(+) create mode 100644 "\351\231\210\346\242\246\346\266\265/20241218\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" create mode 100644 "\351\231\210\346\242\246\346\266\265/20241220\345\256\214\346\225\264\344\273\243\347\240\201.md" diff --git "a/\351\231\210\346\242\246\346\266\265/20241218\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" "b/\351\231\210\346\242\246\346\266\265/20241218\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..e69de29 diff --git "a/\351\231\210\346\242\246\346\266\265/20241220\345\256\214\346\225\264\344\273\243\347\240\201.md" "b/\351\231\210\346\242\246\346\266\265/20241220\345\256\214\346\225\264\344\273\243\347\240\201.md" new file mode 100644 index 0000000..db6ceb0 --- /dev/null +++ "b/\351\231\210\346\242\246\346\266\265/20241220\345\256\214\346\225\264\344\273\243\347\240\201.md" @@ -0,0 +1,201 @@ +## 打开控制台(Controllers)列表 +```cs +using System.Text.Json; +using Blog.Models; +using Microsoft.AspNetCore.Mvc; + +namespace Blog.Controllers; + +public class BlogsController : Controller +{ + private readonly BlogDbContext _db; + public BlogsController() + { + _db=new BlogDbContext(); + } + // 查询 + 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.Author.Contains(Keyword) || x.Content.Contains(Keyword)).ToList(); + return View(list); + } + + } + // 保留一个之前的 + public IActionResult Create(){ + return View(); + } + // 跳转到新增页面 + [HttpPost] + public IActionResult Create(Blogs input) + { + // 保存新添加的数据 + // 找出最大的ID值,然后在它的基础上加一 + var max =_db.Blogs.Select(t=>t.Id).Max(); + input.Id=max+1; + _db.Blogs.Add(input); + // 返回到首页 + return RedirectToAction("Index"); + } + +// 跳转到编辑页面 + + [HttpPost] + [ValidateAntiForgeryToken] + public IActionResult Edit(Blogs input) + { + if(ModelState.IsValid) + { + // 根据传入的id,从数据库中拿到最新的值 + var blog = _db.Blogs.FirstOrDefault(x=>x.Id == input.Id); + + // 判断是否有对应的id记录 + if(blog !=null){ + blog.Title =input.Title; + blog.Author=input.Author; + blog.Content=input.Content; + } + return RedirectToAction("Index"); + } + return View(input); + } + // 根据id去得到内容然后更改 + public IActionResult Edit(int id) + { + var blog = _db.Blogs.FirstOrDefault(x=>x.Id ==id); + // 返回内容 + return View(blog); + } + // 跳转到删除页面 + // 根据id得到需要删除的内容 + public IActionResult Remove(int id) + { + // 定义一个blog保存查询得到的结果 + var blog =_db.Blogs.FirstOrDefault(x=>x.Id==id); + + // 判断查询到的结果是否为空,为空则提示null,不为空则删除 + if(blog!=null){ + return View(blog); + } + return View(); + } + + // 删除的判断 + public IActionResult Require(int id){ + var blog = _db.Blogs.FirstOrDefault(x=>x.Id==id); + + if(blog!=null){ + // 删除数据 + _db.Blogs.Remove(blog); + } + return RedirectToAction("Index"); + + } +} +``` +## 打开视图(Views)列表 +```html + + @* 模型来源 *@ +@model List + + +
+
+
+ @* *@ + @* *@ + @* 查询 *@ +
+ + +
+
+
+ @* *@ + @* 从官网学的 控制器名称 自建名:即跳转的位置*@ + 新增 +
+
+ + + + + + + + + + @foreach (var blog in @Model) + { + + + + + + + + } + + +
ID标题内容作者操作
@blog.Id@blog.Title@blog.Content@blog.Author + @* + *@ + 编辑 + 删除 +
+
+ + @model Blog.Models; + +
+
+
+
+ +
+ + @model Blog.Models.Blogs; + +
+
+
+
+
+ +
+ + + + @model Blog.Models.Blogs; + +

您将要删除以下内容

+ + + + + + + + + + + + + + + + + +
标题:@Model.Title
内容:@Model.Content
作者:@Model.Author
+ 删除 + + 取消 +
+``` \ No newline at end of file -- Gitee