From 9367ea540619e0226aa6c4281693d94f8caaafc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E6=BB=A2=E9=92=B0?= <14091774+well-yes@user.noreply.gitee.com> Date: Sun, 29 Dec 2024 19:15:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=B0=E7=82=B9=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20241223.md" | 36 +++++++++++++++++ .../20241225.md" | 24 ++++++++++++ .../20241226.md" | 39 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 "\351\231\206\346\273\242\351\222\260/20241223.md" create mode 100644 "\351\231\206\346\273\242\351\222\260/20241225.md" create mode 100644 "\351\231\206\346\273\242\351\222\260/20241226.md" diff --git "a/\351\231\206\346\273\242\351\222\260/20241223.md" "b/\351\231\206\346\273\242\351\222\260/20241223.md" new file mode 100644 index 0000000..c2ee2fc --- /dev/null +++ "b/\351\231\206\346\273\242\351\222\260/20241223.md" @@ -0,0 +1,36 @@ +## 学生表新增,编辑功能 + +``` +[HttpPost] + public IActionResult Create(Student input) + { + _db.Students.Add(input); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + public IActionResult Edit(int id) + { + var entity = _db.Students.FirstOrDefault(x => x.Id == id); + if (entity != null) + { + return View(entity); + } + return NotFound(); + } + [HttpPost] + public IActionResult Edit(Student input) + { + var entity = _db.Students.FirstOrDefault(x => x.Id == input.Id); + if (entity != null) + { + entity.StudentCode = input.StudentCode; + entity.StudentName = input.StudentName; + entity.Age = input.Age; + _db.Students.Update(entity); + _db.SaveChanges(); + // _db.SaveChangesAsync(); + return RedirectToAction("Index"); + } + return NotFound(); + } +``` \ No newline at end of file diff --git "a/\351\231\206\346\273\242\351\222\260/20241225.md" "b/\351\231\206\346\273\242\351\222\260/20241225.md" new file mode 100644 index 0000000..85c2e02 --- /dev/null +++ "b/\351\231\206\346\273\242\351\222\260/20241225.md" @@ -0,0 +1,24 @@ +## 学生表删除功能 + +``` +public IActionResult Delete(int id) + { + var obj = _db.Students.FirstOrDefault(x => x.Id == id); + if (obj == null) + { + return NotFound(); + } + return View(obj); + } + public IActionResult DeleteConfirm(int id) + { + var obj = _db.Students.FirstOrDefault(x => x.Id == id); + if (obj == null) + { + return NotFound(); + } + _db.Students.Remove(obj); + _db.SaveChanges(); + return RedirectToAction("Index"); + } +``` \ No newline at end of file diff --git "a/\351\231\206\346\273\242\351\222\260/20241226.md" "b/\351\231\206\346\273\242\351\222\260/20241226.md" new file mode 100644 index 0000000..1d6ba1e --- /dev/null +++ "b/\351\231\206\346\273\242\351\222\260/20241226.md" @@ -0,0 +1,39 @@ +## 学生表查找功能 + +``` +public IActionResult Index(string keyword) + { + // 2种情况 + // 有查找有关键字 + // 没有查找,也没有关键字(关键字为空的情况) + + // 第一种 + // IEnumerable list; + // if (string.IsNullOrEmpty(keyword)) + // { + // list = _db.Students; + // } + // else + // { + // list = _db.Students.Where(x => x.StudentCode.Contains(keyword) || x.StudentName.Contains(keyword)); + // } + // return View(list); + + // 第二种 + // if(string.IsNullOrEmpty(keyword)) + // { + // return View(_db.Students); + // }else{ + // var list=_db.Students.Where(x=>x.StudentCode.Contains(keyword) || x.StudentName.Contains(keyword)); + // return View(list); + // } + + // 第三种 + if (string.IsNullOrEmpty(keyword)) + { + return View(_db.Students); + } + var list = _db.Students.Where(x => x.StudentCode.Contains(keyword) || x.StudentName.Contains(keyword)); + return View(list); + } +``` \ No newline at end of file -- Gitee