diff --git "a/\345\210\230\346\233\246/20241216 -Controller.md" "b/\345\210\230\346\233\246/20241216 -Controller.md" new file mode 100644 index 0000000000000000000000000000000000000000..a53782ce7267dea8f149c1cfa9acc35240815fc5 --- /dev/null +++ "b/\345\210\230\346\233\246/20241216 -Controller.md" @@ -0,0 +1,104 @@ +```js +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using ScoreManager.Models; + +namespace ScoreManager.Controllers; +//学生 +public class StudentsController : Controller +{ + private readonly StudentDbContext _db; + + public StudentsController() + { + _db = new StudentDbContext(); + } + /// + /// 搜索 + /// + /// + [HttpGet] + public IActionResult Index() + { + var list = _db.Students.ToList(); + return View(list); + } + [HttpPost] + public IActionResult Index(string keyword) + { + keyword = string.IsNullOrEmpty(keyword) ? "" : keyword.Trim(); + if(string.IsNullOrEmpty(keyword)) + { + return View(_db.Students); + } + var list = _db.Students.Where(x => x.StudentName.Contains(keyword)).ToList(); + return View(list); + } + /// + /// 新增方法 + /// + /// + [HttpGet] + public IActionResult Create() + { + return View(); + } + [HttpPost] + public IActionResult Create(Student input) + { + //检查 _db.Students 中是否存在至少一个 StudentId 等于 input.StudentId 的学生记录。 + //如果存在(即 Any 方法返回 true),则返回 Create 视图。 + if (_db.Students.Any(x => x.StudentId.Equals(input.StudentId))) + { + return View("Create"); + } + _db.Students.Add(input); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + /// + /// 删除 + /// + /// + public IActionResult Delete(int id) + { + var stu = _db.Students.FirstOrDefault(x => x.Id == id); + return View(stu); + } + public IActionResult DeleteConfirm(int id) + { + var stu = _db.Students.FirstOrDefault(x => x.Id == id); + if(stu != null) + { + _db.Students.Remove(stu); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + return NotFound(); + } + /// + /// 修改 + /// + /// + [HttpGet] + public IActionResult Edit(int id) + { + var stu = _db.Students.FirstOrDefault(x => x.Id == id); + return View(stu); + } + [HttpPost] + public IActionResult Edit(Student input) + { + var stu = _db.Students.FirstOrDefault(x => x.Id == input.Id); + if(stu != null) + { + stu.StudentId = input.StudentId; + stu.StudentName = input.StudentName; + stu.Age = input.Age; + _db.SaveChanges(); + return RedirectToAction("Index"); + } + return View(stu); + } +} +``` \ No newline at end of file diff --git "a/\345\210\230\346\233\246/20241218 -\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/\345\210\230\346\233\246/20241218 -\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..2b8d7047d21b45f7ab4173dc805baab9788aa2e1 --- /dev/null +++ "b/\345\210\230\346\233\246/20241218 -\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,177 @@ +1. 创建一个mvc项目 命令 `dotnet new mvc -o StudentManager` + +2. 模型 Student.cs + ```c# + namespace StudentManager.Models; + public class Student + { + public int Id{get;set;} //主键Id + public string StudentCode{get;set;}=null!; //学号 + public string StudentName{get;set;}=null!; //姓名 + } + ``` + +3. 数据库上下文 StudentDbContext.cs + ```c# + using Microsoft.EntityFrameworkCore; + namespace StudentManager.Models; + public class StudentDbContext:DbContext + { + public DbSetStudents{get;set;}=null!; + + //连接数据库 + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuidler) + { + var str=$"server=SK-20240829LVRN\\SQLEXPRESS;database=StudentDb;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuidler.UseSqlServer(str); + } + } + + + //DbContext:安装依赖包 命令 dotnet add package Microsoft.EntityFrameworkCore.SqlServer + //1. 生成迁移文件 命令 dotnet ef migrations add XXX + 1.1 需要一个工具 ef工具 安装命令 dotnet tool install --global dotnet-ef + 1.2 安装依赖包 命令 dotnet add package Microsoft.EntityFrameworkCore.Design + 1.3 需要程序不能有编译错误,使用 dotnet build 查看是否有编译错误 + 1.4 程序不能处于运行状态 + //2. 将生成的迁移文件更新到数据库中 命令 dotnet ef database update + ``` + +4. 控制器 StudentsController.cs + ```c# + using Microsoft.AspNetCore.Mvc; + using StudentManager.Models; + namespace StudentManager.Controllers; + public class StudentsController:Controller + { + //引用数据库 + private readonly StudentDbContext _db; + public StudentsController() + { + _db=new StudentDbContext(); + } + + public IActionResult Index() + { + return View(); + } + } + ``` +5. 创建对应的视图 Students--Index.cshtml + ```c# + @model List; + +
+
+ +
+ 新增 +
+
+
+ + + + + + + + + @foreach(var item in @Model) + { + + + + + + + + } +
Id姓名性别年龄操作
@item.Id@item.Name@item.Sex@item.Age +
+ 编辑 +
+
+ 删除 +
+
+
+
+ ``` +6. css样式 + ```c# + body{ + display: flex; + justify-content: center; + } + .box + { + width: 350px; + } + .top + { + width: 350px; + display: flex; + margin-top: 5px; + } + .on{ + width: 350px; + } + table,tr,td,th + { + border: 1px solid black; + border-collapse: collapse; + } + table + { + width: 350px; + text-align: center; + } + th{ + background-color: rgb(255, 250, 201); + } + .cz + { + border: none; + display: flex; + justify-content: space-around; + } + a{ + text-decoration: none; + color: white; + } + .add + { + width: 40px; + height: 25px; + text-align: center; + line-height: 25px; + border-radius: 5px; + background-color: rgb(144, 247, 118); + position: relative; + left: 60px; + } + .edit + { + width: 40px; + height: 25px; + text-align: center; + line-height: 25px; + border-radius: 5px; + background-color: rgb(188, 222, 253); + } + .delete + { + width: 40px; + height: 25px; + text-align: center; + line-height: 25px; + border-radius: 5px; + background-color: rgb(241, 171, 226); + } + ``` \ No newline at end of file diff --git "a/\345\210\230\346\233\246/20241219 -\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\2372.md" "b/\345\210\230\346\233\246/20241219 -\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\2372.md" new file mode 100644 index 0000000000000000000000000000000000000000..cd6a9f71c86fd1baa92eace450f418032dd4853d --- /dev/null +++ "b/\345\210\230\346\233\246/20241219 -\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\2372.md" @@ -0,0 +1,161 @@ +### 新增功能 +1. Create.cshtml + ```c# + @model Student; +
+
+
+
+ +
+ ``` +2. 控制器 + ```c# + [HttpPost] + public IActionResult Create(Student input) + { + //将输入的内容添加的students表 + _db.Students.Add(input); + //保存到数据库 + _db.SaveChanges(); + return RedirectToAction("Index"); + } + ``` + +### 修改功能 +1. Edit.cshtml + ```c# + @model Student; +
+
+
+
+ +
+ ``` +2. 控制器 + ```c# + /// + /// 获取修改内容 + /// + /// + /// + public IActionResult Edit(int id) + { + //通过点击传入的ID获取要修改此条数据的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + return View(list); + } + /// + /// 修改页面 + /// + /// + /// + [HttpPost] + public IActionResult Edit(Student input) + { + var list =_db.Students.FirstOrDefault(x=>x.Id==input.Id); + //判断list是否存在 + if(list!=null) + { + //修改的内容要在list中重新赋值 + list.Name=input.Name; + list.Sex=input.Sex; + list.Age=input.Age; + //重新传到数据库 + _db.SaveChanges(); + return RedirectToAction("Index"); + } + //不存在则提示错误 + return NotFound(); + } + ``` + +### 删除功能 +1. Delete.cshtml + ```c# + + + + + + + + + + + + + + + + + +
姓名:@Model.Name
性别:@Model.Sex
年龄:@Model.Age
+ 删除 + + 回到主页 +
+ ``` +2. 控制器 + ```c# + /// + /// 删除页面 + /// + /// + /// + public IActionResult Delete(int id) + { + //通过id获取删除的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + return View(list); + } + /// + /// 确认删除页面 + /// + /// + /// + public IActionResult DeleteConfirm(int id) + { + //通过id获取删除的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + //判断list是否为空 + //list不为空 + if(list!=null) + { + _db.Students.Remove(list); + //数据库的内容也需要删除 + _db.SaveChanges(); + //删除完重定向到index页面 + return RedirectToAction("Index"); + } + //不存在则提示错误 + return NotFound(); + } + ``` + +### 查询功能 +1. 在Index.cshtml中添加 + ```c# +
+ + +
+ ``` +2. 控制器 + ```c# + public IActionResult Index(string keyword) + { + //先判断keyword是否为空,如果不为空删除前后空格 + keyword=string.IsNullOrEmpty(keyword)?"":keyword.Trim(); + //keyword为空,返回在主页 + if(string.IsNullOrEmpty(keyword)) + { + var list=_db.Students.ToList(); + return View(list); + } + var res=_db.Students.Where(x=>x.Name.Contains(keyword)||x.Sex.Contains(keyword)).ToList(); + return View(res); + + } + ``` \ No newline at end of file