From ddca06e9414f7391d521eff3f99fadc41ba5af74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E7=BE=A4?= Date: Sun, 5 Jan 2025 20:18:50 +0800 Subject: [PATCH] =?UTF-8?q?20241230+20250103=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20241230-\347\254\224\350\256\260.md" | 111 ++++++++++++++++++ .../20250103-\347\254\224\350\256\260.md" | 43 +++++++ 2 files changed, 154 insertions(+) create mode 100644 "\350\222\213\347\276\244/20241230-\347\254\224\350\256\260.md" create mode 100644 "\350\222\213\347\276\244/20250103-\347\254\224\350\256\260.md" diff --git "a/\350\222\213\347\276\244/20241230-\347\254\224\350\256\260.md" "b/\350\222\213\347\276\244/20241230-\347\254\224\350\256\260.md" new file mode 100644 index 0000000..59b021e --- /dev/null +++ "b/\350\222\213\347\276\244/20241230-\347\254\224\350\256\260.md" @@ -0,0 +1,111 @@ +```js +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using StudentManger.Models; + +namespace StudentManger.Controllers; + +public class StudentController:Controller{ + + //数据库 +private readonly StudentsContext _db; +public StudentController() +{ + _db=new StudentsContext(); +} + + public IActionResult Index(){ + var list=_db.Students.ToList(); + return View(list); + } + //新增 + public IActionResult Create(){ + return View(); + + } + [HttpPost] + public IActionResult Create(Students input){ + _db.Students.Add(input); + _db.SaveChanges(); + return RedirectToAction("Index"); + + } + + //编辑 + public IActionResult Edit(int id){ + var cont=_db.Students.FirstOrDefault(x=>x.Id==id); + return View(cont); + + } + [HttpPost] + public IActionResult Edit(Students input){ + var cont=_db.Students.FirstOrDefault(x=>x.Id==input.Id); + if(cont==null){ + return NotFound(); + } + cont.Age=input.Age; + cont.StudentName=input.StudentName; + _db.SaveChanges(); + return RedirectToAction("Index"); + + } + //删除 + // public IActionResult Delete(){ + // return View(); + + // } + // public IActionResult DeleteOther(){ + // return View(); + + // } + public IActionResult Delete(int id) + { + var cont = _db.Students.FirstOrDefault(x=>x.Id==id); + + return View(cont); + } + public IActionResult DeleteOther(int id) + { + var cont = _db.Students.FirstOrDefault(x=>x.Id==id); + if(cont==null) + { + return NotFound(); + } + _db.Students.Remove(cont); + _db.SaveChanges(); + + return RedirectToAction("Index"); + + } +} +``` +```js +namespace StudentManger.Models; + +public class Students{ + +public int Id{get;set;} +public int Age{get;set;} +public string StudentName{get;set;}=null!; + +} + +``` +```js +// using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; +namespace StudentManger.Models; + + +public class StudentsContext:DbContext{ + +public DbSet Students{get;set;}=null!; + +protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){ + base.OnConfiguring(optionsBuilder); + var contionString=$"server=.;database=Studentjq;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuilder.UseSqlServer(contionString); +} +} + +``` \ No newline at end of file diff --git "a/\350\222\213\347\276\244/20250103-\347\254\224\350\256\260.md" "b/\350\222\213\347\276\244/20250103-\347\254\224\350\256\260.md" new file mode 100644 index 0000000..f729434 --- /dev/null +++ "b/\350\222\213\347\276\244/20250103-\347\254\224\350\256\260.md" @@ -0,0 +1,43 @@ +### 学生成绩表3 +```js +//删除 + +

你确定删除吗?

+ + + + + + + + + +
课程:@Model.CourseName
+ 确定删除 + + 取消删除
+public IActionResult Delete(int id){ + var obj=_db.Course.FirstOrDefault(x=>x.CourseId==id); + if (obj != null) + { + return View(obj); + } + return View(); + } + + + [HttpPost] + public IActionResult DeleteConfirm(int id){ + var obj=_db.Course.FirstOrDefault(x=>x.CourseId==id); + if(obj!=null){ + return NotFound(); + + + // _db.SaveChange(); + } + + _db.Course.Remove(obj); + return View(obj); + } + +``` -- Gitee