From 7e0c35f3e48efc95862463c78a0fecd792c2e97e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=95=AC=E9=91=AB=28=E6=B5=B4=E7=9A=87=E5=A4=A7?= =?UTF-8?q?=E5=B8=9D=E7=89=88=29?= <3284280335@qq.com> Date: Mon, 30 Dec 2024 16:43:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?20241230-=E6=88=90=E7=BB=A9=E8=A1=A8?= =?UTF-8?q?=E7=9A=84=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...51\350\241\250\346\237\245\350\257\242.md" | 0 ...60\346\225\260\346\215\256\345\272\223.md" | 234 ++++++++++++++++++ 2 files changed, 234 insertions(+) rename "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/202415226-\346\210\220\347\273\251\350\241\250\346\237\245\350\257\242.md" => "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241226-\346\210\220\347\273\251\350\241\250\346\237\245\350\257\242.md" (100%) create mode 100644 "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241230-\346\210\220\347\273\251\350\241\250\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245\345\212\237\350\203\275\343\200\201\345\234\250Linux\345\256\211\350\243\205\346\226\260\346\225\260\346\215\256\345\272\223.md" diff --git "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/202415226-\346\210\220\347\273\251\350\241\250\346\237\245\350\257\242.md" "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241226-\346\210\220\347\273\251\350\241\250\346\237\245\350\257\242.md" similarity index 100% rename from "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/202415226-\346\210\220\347\273\251\350\241\250\346\237\245\350\257\242.md" rename to "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241226-\346\210\220\347\273\251\350\241\250\346\237\245\350\257\242.md" diff --git "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241230-\346\210\220\347\273\251\350\241\250\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245\345\212\237\350\203\275\343\200\201\345\234\250Linux\345\256\211\350\243\205\346\226\260\346\225\260\346\215\256\345\272\223.md" "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241230-\346\210\220\347\273\251\350\241\250\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245\345\212\237\350\203\275\343\200\201\345\234\250Linux\345\256\211\350\243\205\346\226\260\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..4770cfe --- /dev/null +++ "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241230-\346\210\220\347\273\251\350\241\250\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245\345\212\237\350\203\275\343\200\201\345\234\250Linux\345\256\211\350\243\205\346\226\260\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,234 @@ +# 增删改查 +1. 前端 +```html + +@model Score; + +
+ + + + + + + + + + + + + + + + + +
取消保存
+ +
+ +

你确定要删除这个记录吗?

+ + + + + + + + + + + + + + + + + +
课程名称:@Model.CourseName
学生名称:@Model.StudentName
成绩:@Model.Score
确认删除取消删除
+ + @model Score; + +
+ + + + + + + + + + + + + + + + + +
取消保存
+ +
+ + +
+
+ + +
+ 新增 +
+ + + + + + + + + + + + @foreach (var item in Model) + { + + + + + + + + } + + +
Id课程名称学生名称成绩操作
@item.Id@item.CourseName@item.StudentName@item.Scores + 编辑 + 删除 +
+``` +2. 控制器 +```C# +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"); + } +} +``` + +# 在Linux安装新数据库 -- Gitee From 27a6ed4ccc6a9944080143d5e5c92f9bc0e20de7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=95=AC=E9=91=AB=28=E6=B5=B4=E7=9A=87=E5=A4=A7?= =?UTF-8?q?=E5=B8=9D=E7=89=88=29?= <3284280335@qq.com> Date: Mon, 30 Dec 2024 17:41:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?20241230-=E5=9C=A8Linux=E5=AE=89=E8=A3=85?= =?UTF-8?q?=E6=96=B0=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...205\346\226\260\346\225\260\346\215\256\345\272\223.md" | 7 +++++++ 1 file changed, 7 insertions(+) diff --git "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241230-\346\210\220\347\273\251\350\241\250\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245\345\212\237\350\203\275\343\200\201\345\234\250Linux\345\256\211\350\243\205\346\226\260\346\225\260\346\215\256\345\272\223.md" "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241230-\346\210\220\347\273\251\350\241\250\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245\345\212\237\350\203\275\343\200\201\345\234\250Linux\345\256\211\350\243\205\346\226\260\346\225\260\346\215\256\345\272\223.md" index 4770cfe..0dd92bd 100644 --- "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241230-\346\210\220\347\273\251\350\241\250\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245\345\212\237\350\203\275\343\200\201\345\234\250Linux\345\256\211\350\243\205\346\226\260\346\225\260\346\215\256\345\272\223.md" +++ "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241230-\346\210\220\347\273\251\350\241\250\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245\345\212\237\350\203\275\343\200\201\345\234\250Linux\345\256\211\350\243\205\346\226\260\346\225\260\346\215\256\345\272\223.md" @@ -232,3 +232,10 @@ public class ScoresController : Controller ``` # 在Linux安装新数据库 +1. 登录linux +2. 更新一下包 apt update +3. 更新一下系统 apt upgrade -y +4. apt install -y postgresql-common +5. /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh +6. apt update +7. apt -y install postgresql -- Gitee