From cc89b701edb2e2da03e8a9d4810d80270c91a4bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=A4=9A=E9=92=B1?= <3381810463@qq.com> Date: Sun, 29 Dec 2024 21:11:54 +0800 Subject: [PATCH] =?UTF-8?q?20241223=E7=AC=94=E8=AE=B0-=E8=BD=AF=E5=88=A0?= =?UTF-8?q?=E9=99=A4=EF=BC=8C20241225=E7=AC=94=E8=AE=B0-CRUD=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...--\350\275\257\345\210\240\351\231\244.md" | 30 ++++++++ ...56\26017--CRUD\346\223\215\344\275\234.md" | 68 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 "\350\256\270\350\211\263/20241223\347\254\224\350\256\26016--\350\275\257\345\210\240\351\231\244.md" create mode 100644 "\350\256\270\350\211\263/20241225\347\254\224\350\256\26017--CRUD\346\223\215\344\275\234.md" diff --git "a/\350\256\270\350\211\263/20241223\347\254\224\350\256\26016--\350\275\257\345\210\240\351\231\244.md" "b/\350\256\270\350\211\263/20241223\347\254\224\350\256\26016--\350\275\257\345\210\240\351\231\244.md" new file mode 100644 index 0000000..60269e2 --- /dev/null +++ "b/\350\256\270\350\211\263/20241223\347\254\224\350\256\26016--\350\275\257\345\210\240\351\231\244.md" @@ -0,0 +1,30 @@ +```cs +// 在数据表中的代码 true为已删除,false为未删除 +public bool IsDelete{get;set;}=false; + +// 在主控制器中的代码 + public IActionResult Index() + { + var list=studb.Students.Where(x=>x.IsDelete!=true); + return View(list); + } + // 控制器删除中的代码 + public IActionResult Delete(int id) + { + var list=studb.Students.FirstOrDefault(x=>x.Id==id); + if(list==null){ + return NotFound(); + } + return View(list); + } + public IActionResult Require(int id){ + var list=studb.Students.FirstOrDefault(x=>x.Id==id); + if(list==null){ + return NotFound(); + } + _db.IsDelete=true; + studb.Students.Remove(list); + studb.SaveChanges(); + return RedirectToAction("Index") ; + } +``` \ No newline at end of file diff --git "a/\350\256\270\350\211\263/20241225\347\254\224\350\256\26017--CRUD\346\223\215\344\275\234.md" "b/\350\256\270\350\211\263/20241225\347\254\224\350\256\26017--CRUD\346\223\215\344\275\234.md" new file mode 100644 index 0000000..a193249 --- /dev/null +++ "b/\350\256\270\350\211\263/20241225\347\254\224\350\256\26017--CRUD\346\223\215\344\275\234.md" @@ -0,0 +1,68 @@ +```cs +// 控制器中的代码 +using StuInfo.Models; + +using Microsoft.AspNetCore.Mvc; + +namespace StuInfo.Controllers; + +public class StudentInfo:Controller +{ + private readonly StuContext studb; + public StudentInfo() + { + studb=new StuContext(); + } + public IActionResult Index() + { + var list=studb.Students.ToList(); + return View(list); + } + public IActionResult Create() + { + return View(); + } + [HttpPost] + public IActionResult Create(Student input) + { + studb.Add(input); + studb.SaveChanges(); + return RedirectToAction("Index"); + } + public IActionResult Edit(int id) + { + var list=studb.Student.FirstOrDefault(x=>x.id==id) + return View(list); + } + [HttpPost] + public class IActionResult Edit(Student input) + { + var list =studb.Student.FirstOrDefault(x=>x.id=input.id) + if(list==null){ + return NotFound(); + } + list.Name=input.Name; + list.Age=input.Age; + studb.Student.Add(list); + studb.SaveChanges(); + return RedirectToAction("Index"); + } + public IActionResult Delete(int id) + { + var list=studb.Students.FirstOrDefault(x=>x.Id==id); + if(list==null){ + return NotFound(); + } + return View(list); + } + public IActionResult Require(int id){ + var list=studb.Students.FirstOrDefault(x=>x.Id==id); + if(list==null){ + return NotFound(); + } + studb.Students.Remove(list); + studb.SaveChanges(); + return RedirectToAction("Index") ; + } +} +``` \ No newline at end of file -- Gitee