diff --git "a/\351\231\210\344\276\235\346\254\243/20241223--\345\210\240\351\231\244\344\274\252\346\225\260\347\273\204.md" "b/\351\231\210\344\276\235\346\254\243/20241223--\345\210\240\351\231\244\344\274\252\346\225\260\347\273\204.md" new file mode 100644 index 0000000000000000000000000000000000000000..589fdf3cb311954d5d8066e00bbf57e7333797d0 --- /dev/null +++ "b/\351\231\210\344\276\235\346\254\243/20241223--\345\210\240\351\231\244\344\274\252\346\225\260\347\273\204.md" @@ -0,0 +1,65 @@ +1. Action最常用传参: +①查询字符串 +②传id +③复杂传参 + +2. Action返回内容(最常用): +①返回视图 +②重定向 +③直接传值 + +3. 传数据给视图有几种方式: +①简单数据 +②复杂(对象)数据 +③集合数据 + +4. 页面/视图 跳转(交互):asp-action = "Add"(a标签) + +5. 和数据库打交道:dotnet add package Microsoft.EntityFrameworkCore.Design + +6. 指定项目:dotnet watch -project ./StudentManager/score + +7. 在页面删除,数据库不删除 +伪删除:在Models里定义public bool IsDeleted{get;set;} = false +// 是否删除,true为已经删除,false为没有删除 +```cs +public IActionErsult Index(){ + // 过滤条件Linq +} + + public IActionResult Index() + { + // 查找没有删除的过滤,删除了就不要出来了 + var list = _db.course.Where(x=>x.IsDeleted != true); + return View(list); + } +``` +```cs + [HttpPost] + public IActionResult Delete(Course input) + { + var info = _db.course.FirstOrDefault(x=>x.Id==input.Id); + if(info==null){ + return NotFound(); + } + info.IsDeleted = true; + _db.course.Update(info); + _db.SaveChanges(); + return RedirectToAction("Index"); + } +``` +- 在Models/Course里 +```cs +namespace StudentManager.Models; +public class Course +{ + public int Id {get;set;} + public string CourseName {get;set;} = null!; + +/// +/// 是否删除,true为已经删除,false为没删除 +/// 数据库中的0表示没有删除,1代表删除 +/// + public bool IsDeleted {get;set;} = false; +} +``` \ No newline at end of file diff --git "a/\351\231\210\344\276\235\346\254\243/20241225--\346\237\245\350\257\242.md" "b/\351\231\210\344\276\235\346\254\243/20241225--\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..7a9534063f81fa4e7312aadc6f76624ec34283b8 --- /dev/null +++ "b/\351\231\210\344\276\235\346\254\243/20241225--\346\237\245\350\257\242.md" @@ -0,0 +1,54 @@ +### 作业 +1. 在查询框里面显示要查找的内容 +- ![20241229211239](/up-z0.qiniup.com20241229211239.png) + +- 思路: +``` +1. 在 Index 视图,添加一个表单,允许用户输入搜索关键字,并提交到同一个 Index 动作(Action)。 +2. 在 Index 动作中,确保当视图被返回时,abc 参数的值被传递给视图。 +3. 在视图中,使用 ViewBag 或 ViewData 属性来传递 abc 的值,并在表单中设置其值为 abc。 +``` +#### 代码 +```cs + public IActionResult Index(string abc) + { + if (string.IsNullOrEmpty(abc)) + { + return View(_db.course); + } + ViewBag.SearchTerm = abc; // 将abc的值传递给ViewBag + + var list = _db.course.Where(x => !x.IsDeleted); + var res = list.Where(x => x.CourseName.Contains(abc)); + return View(res); + } +``` +- 在index.cshtml +```cs +@model IEnumerable; +@{ + ViewBag.Title = "Index"; +} +
+
+ + +
+
+``` + +### 笔记 +- 查询的另一种写法 +```cs + IEnumerable list; + if (string.IsNullOrEmpty(abc)) + { + list = _db.Students; + } + else + { + var tmp = _db.Students.Where(x => !x.IsDeleted); + list = tmp.Where(x => x.StudentCode.Contains(abc) || x.StudentName.Contains(abc)); + } + return View(list); +```