From a71d7d9d49e54044e6532f544224130880894b9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=A9=89=E5=A9=B7?= <2465898445@qq.com> Date: Thu, 2 Jan 2025 21:59:13 +0800 Subject: [PATCH] tijiao --- ...6\347\224\237\347\256\241\347\220\2061.md" | 57 ++++ ...6\347\224\237\347\256\241\347\220\2062.md" | 99 ++++++ ...41\347\220\206\347\263\273\347\273\237.md" | 313 ++++++++++++++++++ ...65\351\235\242\346\223\215\344\275\234.md" | 68 ++++ 4 files changed, 537 insertions(+) create mode 100644 "\347\216\213\345\251\211\345\251\267/20241223-\345\255\246\347\224\237\347\256\241\347\220\2061.md" create mode 100644 "\347\216\213\345\251\211\345\251\267/20241225-\345\255\246\347\224\237\347\256\241\347\220\2062.md" create mode 100644 "\347\216\213\345\251\211\345\251\267/20241226-\347\256\241\347\220\206\347\263\273\347\273\237.md" create mode 100644 "\347\216\213\345\251\211\345\251\267/20241230-\351\241\265\351\235\242\346\223\215\344\275\234.md" diff --git "a/\347\216\213\345\251\211\345\251\267/20241223-\345\255\246\347\224\237\347\256\241\347\220\2061.md" "b/\347\216\213\345\251\211\345\251\267/20241223-\345\255\246\347\224\237\347\256\241\347\220\2061.md" new file mode 100644 index 0000000..e2fefa4 --- /dev/null +++ "b/\347\216\213\345\251\211\345\251\267/20241223-\345\255\246\347\224\237\347\256\241\347\220\2061.md" @@ -0,0 +1,57 @@ +# 创建一个学生管理系统涉及到前端和后端的集成开发。 +## 1. 创建表 +``` +dotnet new webapi -n StudentManagementSystem +cd StudentManagementSystem +``` +## 2. 安装Entity Framework Core +安装Entity Framework Core用于数据库操作: + +``` +dotnet add package Microsoft.EntityFrameworkCore.SqlServer +dotnet add package Microsoft.EntityFrameworkCore.Design +``` +## 3. 定义数据库模型 +在Models文件夹中创建一个Student.cs文件: + +csharp +public class Student +{ + public int Id { get; set; } + public string Name { get; set; } + public int Age { get; set; } + public string Grade { get; set; } +} +## 4. 配置数据库上下文 +在Data文件夹中创建一个ApplicationDbContext.cs文件: + +```csharp +using Microsoft.EntityFrameworkCore; + +public class ApplicationDbContext : DbContext +{ + public ApplicationDbContext(DbContextOptions options) + : base(options) + { + } + + public DbSet Students { get; set; } +} +``` +5. 配置数据库连接字符串 +在appsettings.json文件中添加数据库连接字符串: +``` +json +{ + "ConnectionStrings": { + "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=StudentManagement;Trusted_Connection=True;MultipleActiveResultSets=true" + } +} +``` +6. 数据库迁移 +创建数据库迁移并更新数据库: + +``` +dotnet ef migrations add InitialCreate +dotnet ef database update +``` \ No newline at end of file diff --git "a/\347\216\213\345\251\211\345\251\267/20241225-\345\255\246\347\224\237\347\256\241\347\220\2062.md" "b/\347\216\213\345\251\211\345\251\267/20241225-\345\255\246\347\224\237\347\256\241\347\220\2062.md" new file mode 100644 index 0000000..d0103a8 --- /dev/null +++ "b/\347\216\213\345\251\211\345\251\267/20241225-\345\255\246\347\224\237\347\256\241\347\220\2062.md" @@ -0,0 +1,99 @@ +# 创建一个学生管理系统涉及到前端和后端的集成开发。 + +## 7. 创建API控制器 + +在`Controllers`文件夹中创建一个`StudentsController.cs`文件: + +```csharp +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +[Route("api/[controller]")] +[ApiController] +public class StudentsController : ControllerBase +{ + private readonly ApplicationDbContext _context; + + public StudentsController(ApplicationDbContext context) + { + _context = context; + } + + // GET: api/Students + [HttpGet] + public async Task>> GetStudents() + { + return await _context.Students.ToListAsync(); + } + + // POST: api/Students + [HttpPost] + public async Task> PostStudent(Student student) + { + _context.Students.Add(student); + await _context.SaveChangesAsync(); + + return CreatedAtAction("GetStudent", new { id = student.Id }, student); + } +} +``` + +## 8. 前端页面 + +创建一个简单的HTML页面用于显示和添加学生信息: + +```html + + + + + Student Management System + + +

Student Management System

+
+ + + + +
+
    + + + + +``` + + diff --git "a/\347\216\213\345\251\211\345\251\267/20241226-\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/\347\216\213\345\251\211\345\251\267/20241226-\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000..d792789 --- /dev/null +++ "b/\347\216\213\345\251\211\345\251\267/20241226-\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,313 @@ +# 控制器 +## - CoursesController.cs +```csharp +using Microsoft.AspNetCore.Mvc; +using ScoreManager.Models; + +namespace ScoreManager.Controllers; + +public class CoursesController : Controller +{ + private readonly ScoreDbContext _db; + public CoursesController() + { + _db = new ScoreDbContext(); + } + public IActionResult Index(string keyword) + { + keyword = string.IsNullOrEmpty(keyword) ? "" : keyword.Trim(); + if (string.IsNullOrEmpty(keyword)) + { + var list = _db.Courses.ToList(); + return View(list); + } + var res = _db.Courses.Where(x => x.CourseName.Contains(keyword)).ToList(); + return View(res); + + } + public IActionResult Edit(int id) + { + var obj = _db.Courses.FirstOrDefault(x => x.Id == id); + return View(obj); + } + + [HttpPost] + public IActionResult Edit(Course input) + { + var obj = _db.Courses.FirstOrDefault(x => x.Id == input.Id); + if (obj == null) + { + return NotFound(); + } + obj.CourseName = input.CourseName; + _db.SaveChanges(); + return RedirectToAction("Index"); + } + public IActionResult Delete(int id) + { + var obj = _db.Courses.FirstOrDefault(x => x.Id == id); + return View(obj); + } + public IActionResult DeleteConfirm(int id) + { + var obj = _db.Courses.FirstOrDefault(x => x.Id == id); + if (obj == null) + { + return NotFound(); + } + _db.Courses.Remove(obj); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + public IActionResult Create() + { + return View(); + } + + [HttpPost] + public IActionResult Create(Course input) + { + _db.Courses.Add(input); + _db.SaveChanges(); + return RedirectToAction("Index"); + } +} +``` + +## - ScoresController.cs +```csharp +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using ScoreManager.Models; + +namespace ScoreManager.Controllers; + +public class ScoresController : Controller +{ + private readonly ScoreDbContext _db; + + public ScoresController() + { + _db = new ScoreDbContext(); + } + public IActionResult Index(string keyword) + { + var list = _db.Scores.ToList(); + var stus = _db.Students.Where(x => !x.IsDeleted).ToList(); + var cours = _db.Courses.ToList(); + var res = list.Select(x => + { + var tmpStu = stus.FirstOrDefault(y => y.Id == x.StudentId); + var tmpCourse = cours.FirstOrDefault(z => z.Id == x.CourseId); + return new + { + x.Id, + x.StudentId, + StudentName = tmpStu == null ? "" : tmpStu.StudentName, + x.CourseId, + CourseName = tmpCourse == null ? "" : tmpCourse.CourseName, + x.Scores + }; + }); + + if (string.IsNullOrEmpty(keyword)) + { + return View(res); + } + ViewBag.Keyword=keyword; + var tmpList = res.Where(x => x.StudentName.Contains(keyword) || x.CourseName.Contains(keyword) + || x.Scores.ToString().Contains(keyword)); + return View(tmpList); + + } + + public IActionResult Create() + { + var students = _db.Students.Where(x => !x.IsDeleted).ToList(); + var course = _db.Courses.ToList(); + ViewBag.Students = new SelectList(students, "Id", "StudentName"); + ViewBag.Courses = new SelectList(course, "Id", "CourseName"); + return View(); + } + [HttpPost] + public IActionResult Create(Score input) + { + _db.Scores.Add(input); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + public IActionResult Edit(int id) + { + var obj = _db.Scores.FirstOrDefault(x => x.Id == id); + if (obj == null) + { + return NotFound(); + } + var students = _db.Students.Where(x => !x.IsDeleted).ToList(); + var course = _db.Courses.ToList(); + ViewBag.Students = new SelectList(students, "Id", "StudentName"); + ViewBag.Courses = new SelectList(course, "Id", "CourseName"); + return View(obj); + } + [HttpPost] + public IActionResult Edit(Score input) + { + var obj = _db.Scores.FirstOrDefault(x => x.Id == input.Id); + if (obj != null) + { + obj.CourseId = input.CourseId; + obj.StudentId = input.StudentId; + obj.Scores = input.Scores; + _db.Scores.Update(obj); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + return NotFound(); + } + public IActionResult Delete(int id) + { + var tmp = _db.Scores.FirstOrDefault(x => x.Id == id); + if (tmp == null) + { + return NotFound(); + } + // 使用非空的成绩记录,在课程表中查找对应id的课程信息(存在找不到的情况) + var tmpCourse = _db.Courses.FirstOrDefault(x => x.Id == tmp.CourseId); + var tmpStudent = _db.Students.FirstOrDefault(x => x.Id == tmp.StudentId); + + // 处理有可能找不到的课程或者学生的情况 + var cName = tmpCourse == null ? "" : tmpCourse.CourseName; + var sName = tmpStudent == null ? "" : tmpStudent.StudentName; + + var obj = new { tmp.Id, tmp.CourseId, CourseName = cName, tmp.StudentId, StudentName = sName, Score = tmp.Scores }; + return View(obj); + } + + public IActionResult DeleteConfirm(int id) + { + var obj = _db.Scores.FirstOrDefault(x => x.Id == id); + if (obj == null) + { + return NotFound(); + } + + _db.Scores.Remove(obj); + _db.SaveChanges(); + + return RedirectToAction("Index"); + } +} +``` +## - StudentsController.cs +```csharp +using System.Text.Json; +using Microsoft.AspNetCore.Mvc; +using ScoreManager.Models; + +namespace ScoreManager.Controllers; + +public class StudentsController : Controller +{ + private readonly ScoreDbContext _db; + public StudentsController() + { + _db = new ScoreDbContext(); + } + + 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); + // } + + // 第三种 + var res = _db.Students.Where(x => !x.IsDeleted); + if (string.IsNullOrEmpty(keyword)) + { + return View(res); + } + var list = res.Where(x => x.StudentCode.Contains(keyword) || x.StudentName.Contains(keyword)); + return View(list); + } + [HttpGet] + public IActionResult Create() + { + return View(); + } + [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(); + } + 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(); + } + obj.IsDeleted = true; + _db.Students.Update(obj); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + +} +``` \ No newline at end of file diff --git "a/\347\216\213\345\251\211\345\251\267/20241230-\351\241\265\351\235\242\346\223\215\344\275\234.md" "b/\347\216\213\345\251\211\345\251\267/20241230-\351\241\265\351\235\242\346\223\215\344\275\234.md" new file mode 100644 index 0000000..67b4c44 --- /dev/null +++ "b/\347\216\213\345\251\211\345\251\267/20241230-\351\241\265\351\235\242\346\223\215\344\275\234.md" @@ -0,0 +1,68 @@ + ## - student index.cshtml + ```csharp + @model IEnumerable; + + +
    +
    + + +
    + 新增 +
    + + + + + + + + + + + + @foreach (var item in Model) + { + + + + + + + + } + + +
    Id学号学生姓名年龄操作
    @item.Id@item.StudentCode@item.StudentName@item.Age + 编辑 + 删除 +
    +``` + +## create.cshtml +```csharp +@model Student; + +
    + + + + + + + + + + + + + + + + + +
    取消
    + + +
    +``` \ No newline at end of file -- Gitee