From abce0dafeb7e2041c86078891fa391099e54e849 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E6=9B=A6?= <2196258247@qq.com>
Date: Sun, 22 Dec 2024 19:44:51 +0800
Subject: [PATCH 1/3] =?UTF-8?q?20241216=E7=AC=94=E8=AE=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../20241216 -Controller.md" | 104 ++++++++++++++++++
...41\347\220\206\347\263\273\347\273\237.md" | 0
2 files changed, 104 insertions(+)
create mode 100644 "\345\210\230\346\233\246/20241216 -Controller.md"
create mode 100644 "\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"
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 0000000..a53782c
--- /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 0000000..e69de29
--
Gitee
From 33a1e185f5c552ae4b5242e466af370177ec1095 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E6=9B=A6?= <2196258247@qq.com>
Date: Sun, 22 Dec 2024 19:45:47 +0800
Subject: [PATCH 2/3] =?UTF-8?q?20241218=E7=AC=94=E8=AE=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...41\347\220\206\347\263\273\347\273\237.md" | 177 ++++++++++++++++++
1 file changed, 177 insertions(+)
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"
index e69de29..2b8d704 100644
--- "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"
@@ -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;
+
+
+
+
+
+
+ Id |
+ 姓名 |
+ 性别 |
+ 年龄 |
+ 操作 |
+
+ @foreach(var item in @Model)
+ {
+
+ @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
--
Gitee
From 33b096dd691341327803e37ca2c4300b4ad3bedf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E6=9B=A6?= <2196258247@qq.com>
Date: Sun, 22 Dec 2024 19:46:57 +0800
Subject: [PATCH 3/3] =?UTF-8?q?20241219=E7=AC=94=E8=AE=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...1\347\220\206\347\263\273\347\273\2372.md" | 161 ++++++++++++++++++
1 file changed, 161 insertions(+)
create mode 100644 "\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"
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 0000000..cd6a9f7
--- /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
--
Gitee