From 8989e5fbc61eecc02b61123e34ae7bf16e1d2ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=AC=A2?= <3167389063@qq.com> Date: Sun, 29 Dec 2024 20:51:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=88=90=E7=BB=A9=E8=A1=A8=E5=92=8C=E5=AD=A6?= =?UTF-8?q?=E7=94=9F=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...--\346\210\220\347\273\251\350\241\250.md" | 80 +++++++++++++ ...--\345\255\246\347\224\237\350\241\250.md" | 111 ++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 "\345\210\230\346\254\242/20241223--\346\210\220\347\273\251\350\241\250.md" create mode 100644 "\345\210\230\346\254\242/20241225--\345\255\246\347\224\237\350\241\250.md" diff --git "a/\345\210\230\346\254\242/20241223--\346\210\220\347\273\251\350\241\250.md" "b/\345\210\230\346\254\242/20241223--\346\210\220\347\273\251\350\241\250.md" new file mode 100644 index 0000000..83ac59b --- /dev/null +++ "b/\345\210\230\346\254\242/20241223--\346\210\220\347\273\251\350\241\250.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 diff --git "a/\345\210\230\346\254\242/20241225--\345\255\246\347\224\237\350\241\250.md" "b/\345\210\230\346\254\242/20241225--\345\255\246\347\224\237\350\241\250.md" new file mode 100644 index 0000000..59b021e --- /dev/null +++ "b/\345\210\230\346\254\242/20241225--\345\255\246\347\224\237\350\241\250.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