From e2d4fac23afb2e5f0762c897a1688fad1a128cea 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, 5 Jan 2025 21:13:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B018=E3=80=81=E7=AC=94=E8=AE=B0?= =?UTF-8?q?19?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...56\26018--CRUD\346\223\215\344\275\234.md" | 87 +++++++++++++++++++ ...56\26019--CRUD\346\223\215\344\275\234.md" | 55 ++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 "\350\256\270\350\211\263/20241230\347\254\224\350\256\26018--CRUD\346\223\215\344\275\234.md" create mode 100644 "\350\256\270\350\211\263/20250103\347\254\224\350\256\26019--CRUD\346\223\215\344\275\234.md" diff --git "a/\350\256\270\350\211\263/20241230\347\254\224\350\256\26018--CRUD\346\223\215\344\275\234.md" "b/\350\256\270\350\211\263/20241230\347\254\224\350\256\26018--CRUD\346\223\215\344\275\234.md" new file mode 100644 index 0000000..5e48828 --- /dev/null +++ "b/\350\256\270\350\211\263/20241230\347\254\224\350\256\26018--CRUD\346\223\215\344\275\234.md" @@ -0,0 +1,87 @@ +```cs + +using Microsoft.AspNetCore.Mvc; +using StuInfo.Models; +namespace StuInfo.Controllers; +public class StuDb : Controller +{ + private readonly StuDbContext studb; + public StuDb() + { + studb=new StuDbContext(); + } + public IActionResult Index(string keyword) + { + // 如果为空返回视图 + if(string.IsNullOrEmpty(keyword)){ + var list = studb.Students.ToList(); + return View(list); + } + var res=studb.Students.Where(x=>x.Name.Contains(keyword)).ToList(); + return View(res); + } + // 新增页面 + public IActionResult Create() + { + return View(); + + + } + [HttpPost] + public IActionResult Create(Student input) + { + var list=new Student + { + Name=input.Name, + Age=input.Age + }; + studb.Students.Add(list); + studb.SaveChanges(); + return RedirectToAction("Index"); + } + + // 编辑页面 + public IActionResult Edit(int id) + { + var list=studb.Students.FirstOrDefault(x=>x.Id == id); + if(list == null){ + return NotFound(); + } + return View(list); + } + [HttpPost] + public IActionResult Edit(Student input){ + var list = studb.Students.FirstOrDefault(x=>x.Id == input.Id); + if(list==null){ + return NotFound(); + } + list.Name=input.Name; + list.Age=input.Age; + studb.Students.Update(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 Dc(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 diff --git "a/\350\256\270\350\211\263/20250103\347\254\224\350\256\26019--CRUD\346\223\215\344\275\234.md" "b/\350\256\270\350\211\263/20250103\347\254\224\350\256\26019--CRUD\346\223\215\344\275\234.md" new file mode 100644 index 0000000..1f466c7 --- /dev/null +++ "b/\350\256\270\350\211\263/20250103\347\254\224\350\256\26019--CRUD\346\223\215\344\275\234.md" @@ -0,0 +1,55 @@ +```cs +namespace StuInfo.Controllers; + +using Microsoft.AspNetCore.Mvc; +using StuInfo.Models; + +public class StudentController : Controller +{ + private readonly StuDbContext stu; + + public StudentController(){ + stu =new StuDbContext(); + } + + public IActionResult Index(){ + var list = stu.Students.ToList(); + return View(list); + } + + public IActionResult Create(){ + return View(); + } + [HttpPost] + public IActionResult Create(Student input) + { + var list =new Student{ + StuName = input.StuName, + Age = input.Age + }; + stu.Students.Add(list); + stu.SaveChanges(); + return RedirectToAction("Index"); + } + public IActionResult Edit(int id){ + var list=stu.Students.FirstOrDefault(x=>x.Id==id); + if(list == null){ + return NotFound(); + } + return View(list); + } + [HttpPost] + public IActionResult Edit(Student input){ + var list=stu.Students.FirstOrDefault(x=>x.Id==input.Id); + if(list == null){ + return NotFound(); + } + list.StuName=input.StuName; + list.Age=input.Age; + stu.Students.Update(list); + stu.SaveChanges(); + return RedirectToAction("Index"); + } + +} +``` \ No newline at end of file -- Gitee