From e239f47a953da445940553cb7ac7c3cb58ea8374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=88=90=E8=B1=AA?= <1400383615@qq.com> Date: Sun, 22 Dec 2024 20:22:43 +0800 Subject: [PATCH] 20241222 --- ...272\224\347\224\250EntityFrameworkCore.md" | 5 +- .../20241216-Controller.md" | 104 ++++++++++++++ .../20241218-Model.md" | 36 +++++ .../20241219-Views.md" | 131 ++++++++++++++++++ 4 files changed, 274 insertions(+), 2 deletions(-) create mode 100644 "\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241216-Controller.md" create mode 100644 "\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241218-Model.md" create mode 100644 "\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241219-Views.md" diff --git "a/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" index 32e444e..513976e 100644 --- "a/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" +++ "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" @@ -1,4 +1,4 @@ -1. 安装依赖包:`dotnet add package Microsoft.EntityFramewordCore.SqlServer` +1. 安装依赖包:`dotnet add package Microsoft.EntityFrameworkCore.SqlServer` 2. 定义数据库表模型 ```csharp @@ -53,5 +53,6 @@ dotnet ef migrations add Init * ActionResult类型 将一般数据类型和HTTP状态信息混合使用 * 特定于格式的操作结果:如JsonResult和ContentResult * POCO(普通旧CLR对象) -@endmindmap + @endmindmap +`dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 9.0.0 --source "地址"` diff --git "a/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241216-Controller.md" "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241216-Controller.md" new file mode 100644 index 0000000..a53782c --- /dev/null +++ "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/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/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241218-Model.md" "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241218-Model.md" new file mode 100644 index 0000000..6d66eeb --- /dev/null +++ "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241218-Model.md" @@ -0,0 +1,36 @@ +```js +//DbContext +using Microsoft.EntityFrameworkCore; + +namespace ScoreManager.Models; + +public class StudentDbContext : DbContext +{ + public DbSet Students {get;set;} = null!; + + public DbSet Courses {get;set;} = null!; + + public DbSet Scores {get;set;} = null!; + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + + var onString = $"server=.\\SQLEXPRESS;database=Db2;uid=sa;pwd=123456;TrustServerCertificate=true;"; + + optionsBuilder.UseSqlServer(onString); + } +} +``` +```js +//Student +namespace ScoreManager.Models; + +public class Student +{ + public int Id {get;set;} + public int Age {get;set;} + public int StudentId {get;set;} + public string StudentName {get;set;} = null!; +} +``` \ No newline at end of file diff --git "a/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241219-Views.md" "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241219-Views.md" new file mode 100644 index 0000000..10f8862 --- /dev/null +++ "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241219-Views.md" @@ -0,0 +1,131 @@ +```html + + +@model List + + + +
+
+ + + 新增 +
+ + + + + + + + @foreach (var item in Model) + { + + + + + + + } +
Id年龄姓名操作
@item.Id@item.Age@item.StudentName + 编辑 + 删除 +
+``` + +```html + +@model ScoreManager.Models.Student; + + + +@using(Html.BeginForm("Edit","Students",FormMethod.Post)) +{ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + 取消 +
+} +``` + +```html + +@model ScoreManager.Models.Student + + + +@using(Html.BeginForm("DeleteConfirm","Students",FormMethod.Post)) +{ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + 取消 +
+} +``` + +```html + +@model ScoreManager.Models.Student + + +@using(Html.BeginForm("Create","Students",FormMethod.Post)) +{ +
+ + +
+
+ + +
+
+ + +
+
+ + 取消 +
+} +``` \ No newline at end of file -- Gitee