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 0000000000000000000000000000000000000000..c2ee2fce924e53c5350abdb4c90e6dd8df1dea05 --- /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 0000000000000000000000000000000000000000..85c2e02e9cc4a6446f3b1a853e964066e3a97b08 --- /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 0000000000000000000000000000000000000000..1d6ba1ecc292fe1b3dea5340df2d06f033ca50f4 --- /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