diff --git "a/\346\235\250\350\224\232\344\270\234/\347\254\224\350\256\260/mvc\347\254\224\350\256\26010.md" "b/\346\235\250\350\224\232\344\270\234/\347\254\224\350\256\260/mvc\347\254\224\350\256\26010.md" new file mode 100644 index 0000000000000000000000000000000000000000..7ee598d4ab9032211f06e62523050705e821312c --- /dev/null +++ "b/\346\235\250\350\224\232\344\270\234/\347\254\224\350\256\260/mvc\347\254\224\350\256\26010.md" @@ -0,0 +1,36 @@ +### 连表查询18题 + ```C# + var list =students.Select(stu=> + { + //查找当前学生Id对应的课程,只选择课程名称 + var temCourse=Course.Where(x=>x.StudentId=stu.Id).Select(x=>x.CourseName); + //将一个字符串集合变成一个以逗号间隔的字符串 + var str=string.Join(",",list); + //重新组合学生信息和课程字符串信息,准备返回 + var res=new{stu.Id,stu.Name,stu.Age,CourseName=str}; + return res; + } + //过滤下没有选修课程的记录 + var newlist = list.Where(x=>x.CourseName.Length>0); + //打印学生信息 + foreach (var item in newlist){ + cw($"{item.Id}-{item.Name}-{item.Age}-{item.CourseName}") + } + + ) + ``` +### 真实数据库 + + ```C# + //readonly 只读 + private readonly BlogContext _db; + + //构造函数 + + public BlogContext() + { + _db=new BlogContext(); + } + + //保存到数据库 + _db.SaveChanges(); \ No newline at end of file diff --git "a/\346\235\250\350\224\232\344\270\234/\347\254\224\350\256\260/mvc\347\254\224\350\256\26011.md" "b/\346\235\250\350\224\232\344\270\234/\347\254\224\350\256\260/mvc\347\254\224\350\256\26011.md" new file mode 100644 index 0000000000000000000000000000000000000000..086210e6637a6f594f7a9df5d22ca618e3fc5d39 --- /dev/null +++ "b/\346\235\250\350\224\232\344\270\234/\347\254\224\350\256\260/mvc\347\254\224\350\256\26011.md" @@ -0,0 +1,104 @@ +创建一个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 +
+ 编辑 +
+
+ 删除 +
+
+
+
\ No newline at end of file diff --git "a/\346\235\250\350\224\232\344\270\234/\347\254\224\350\256\260/mvc\347\254\224\350\256\26012.md" "b/\346\235\250\350\224\232\344\270\234/\347\254\224\350\256\260/mvc\347\254\224\350\256\26012.md" new file mode 100644 index 0000000000000000000000000000000000000000..ac1c6c2ee5f2e0ffd40609a9015cdb5fdf4bb145 --- /dev/null +++ "b/\346\235\250\350\224\232\344\270\234/\347\254\224\350\256\260/mvc\347\254\224\350\256\26012.md" @@ -0,0 +1,160 @@ +### 新增功能 +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