From 19c6b3b73c4a1652fcf6fe519fb2609dab471897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 29 Dec 2024 13:25:05 +0000 Subject: [PATCH 1/2] 202241223 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈伟鑫 <14089910+cwx050211@user.noreply.gitee.com> --- .../202241223.md" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "\351\231\210\344\274\237\351\221\253/202241223.md" diff --git "a/\351\231\210\344\274\237\351\221\253/202241223.md" "b/\351\231\210\344\274\237\351\221\253/202241223.md" new file mode 100644 index 0000000..b0aa378 --- /dev/null +++ "b/\351\231\210\344\274\237\351\221\253/202241223.md" @@ -0,0 +1,80 @@ +```cs +public class CoursesController:Controller +{ + private readonly ScoreDbContext _db; + public CoursesController() + { + _db = new ScoreDbContext(); + } + public IActionResult Index(string keyword) + { + 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 Create() + { + return View(); + } + [HttpPost] + public IActionResult Create(Course input) + { + var entity = new Course + { + CourseName =input.CourseName + }; + + _db.Courses.Add(entity); + _db.SaveChanges(); + + return RedirectToAction("Index"); + } + + public IActionResult Edit(int id) + { + var obj = _db.Courses.FirstOrDefault(x => x.Id == id); + if(obj== null) + { + return NotFound(); + } + 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.Courses.Update(obj); + _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"); + } +} +``` \ No newline at end of file -- Gitee From 6fe92bd2d17f7cc661dea331b9f98b0b6bd7696f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 29 Dec 2024 13:25:31 +0000 Subject: [PATCH 2/2] 202241225 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈伟鑫 <14089910+cwx050211@user.noreply.gitee.com> --- .../202241225.md" | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 "\351\231\210\344\274\237\351\221\253/202241225.md" diff --git "a/\351\231\210\344\274\237\351\221\253/202241225.md" "b/\351\231\210\344\274\237\351\221\253/202241225.md" new file mode 100644 index 0000000..8b87675 --- /dev/null +++ "b/\351\231\210\344\274\237\351\221\253/202241225.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 -- Gitee