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 0000000000000000000000000000000000000000..60269e2c58e6acad09ff9b4a61f0029f03c362df --- /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 0000000000000000000000000000000000000000..a193249e9fd117eda708666d40be3b45720fda18 --- /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