diff --git "a/\350\222\213\347\276\244/20241223-\344\274\252\346\225\260\347\273\204.md" "b/\350\222\213\347\276\244/20241223-\344\274\252\346\225\260\347\273\204.md" new file mode 100644 index 0000000000000000000000000000000000000000..53c437018db4247639ae4cbeb7ed3020dee096c1 --- /dev/null +++ "b/\350\222\213\347\276\244/20241223-\344\274\252\346\225\260\347\273\204.md" @@ -0,0 +1,11 @@ +### mvc伪数组 +- 数组是一种数据结构,用于存储固定大小的同类型元素集合。 +- 数组的大小在声明时确定,并且之后不能改变(除非使用特定的方法,如Array.Resize,但这会创建一个新的数组)。 +- 数组通过索引访问元素,索引从0开始。 + +- List作为伪数组: +- List是C#中最常用的伪数组实现。 +- 它提供了与数组类似的索引访问方式,但List的大小是动态的,可以根据需要增加或减少元素。 +List还提供了许多方便的方法,如Add、Remove、Insert等,用于操作集合中的元素。 + +- Dictionary或SortedDictionary。 \ No newline at end of file diff --git "a/\350\222\213\347\276\244/20241225-\345\255\246\347\224\237\345\210\206\346\225\260\350\241\250.md" "b/\350\222\213\347\276\244/20241225-\345\255\246\347\224\237\345\210\206\346\225\260\350\241\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..59b021e4b8887e8a7d8efdc56e67c004a6b915b6 --- /dev/null +++ "b/\350\222\213\347\276\244/20241225-\345\255\246\347\224\237\345\210\206\346\225\260\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