戳戳猫的小窝
更新日志
关于
进入编辑视图前,需要获取此学生的ClassId,MajorId,GradeId以在进入视图后下拉列表能处于正确的选项。 # 添加GetWithClass方法 在StudentRepo中添加GetWithClass方法: 代码如下: ``` public Student? GetWithClass(Guid id) { return DbSet.Include(s => s.Class).SingleOrDefault(s => s.Id == id); } ``` 这样获取的学生实体附带Class的数据。 # 添加GetEditModel方法 在StudentService中添加GetEditModel方法以获取学生实体及下拉列表数据。 代码如下: ``` public StudentViewModel GetEditModel(Guid id) { Student? student = studentRepo.GetWithClass(id); if (student == null) { throw new NullReferenceException(); } StudentViewModel model = new StudentViewModel() { Id = student.Id, Number = student.Number, Name = student.Name, Gender = student.Gender, Birthday = student.Birthday, ClassId = student.ClassId, MajorId = student.Class.MajorId, GradeId = student.Class.GradeId, }; StudentViewModel selectListModel = GetSelectListModel(); model.Majors = selectListModel.Majors; model.Grades = selectListModel.Grades; return model; } ``` # 创建Edit动作 在StudentController中创建Edit动作,代码如下: ``` public IActionResult Edit(Guid id) { StudentViewModel model; try { model = studentService.GetEditModel(id); } catch (NullReferenceException) { return NotFound(); } return View(model); } ``` # 创建Edit视图 创建Edit.cshtml,代码如下: ``` @{ ViewBag.Title = "编辑学生"; } @model StudentViewModel <form method="post" class="mx-auto" style="width:320px;"> <div asp-validation-summary="All" class="has-text-danger mb-4"></div> <input asp-for="Id" hidden> <div class="field"> <label asp-for="MajorName" class="label"></label> <div class="control"> <div class="select is-fullwidth"> <select asp-for="MajorId" asp-items="Model.Majors" hx-get="/Student/GetClassSelectPartial" hx-target="#ClassId" hx-include="#GradeId,#ClassId" hx-swap="outerHTML"> <option value="">请选择</option> </select> </div> </div> </div> <div class="field"> <label asp-for="GradeName" class="label"></label> <div class="control"> <div class="select is-fullwidth"> <select asp-for="GradeId" asp-items="Model.Grades" hx-get="/Student/GetClassSelectPartial" hx-target="#ClassId" hx-include="#MajorId,#ClassId" hx-swap="outerHTML"> <option value="">请选择</option> </select> </div> </div> </div> <div class="field"> <label asp-for="ClassName" class="label"></label> <div class="control"> <div class="select is-fullwidth"> <input asp-for="ClassId" hx-get="/Student/GetClassSelectPartial" hx-trigger="load" hx-include="#MajorId,#GradeId" hx-swap="outerHTML"> </div> </div> </div> <div class="field"> <label asp-for="Number" class="label"></label> <div class="control"> <input asp-for="Number" class="input"> </div> </div> <div class="field"> <label asp-for="Name" class="label"></label> <div class="control"> <input asp-for="Name" class="input"> </div> </div> <div class="field"> <label asp-for="Birthday" class="label"></label> <div class="control"> <input asp-for="Birthday" class="input"> </div> </div> <div class="field"> <label asp-for="Gender" class="label"></label> <div class="control"> <label class="radio"> <input type="radio" asp-for="Gender" value="false"> 男 </label> <label class="radio"> <input type="radio" asp-for="Gender" value="true" > 女 </label> </div> </div> <div class="field"> <button class="button is-primary is-fullwidth">提交</button> </div> </form> ``` # 添加Update方法 在StudentService中添加Update方法,代码如下: ``` public void Update(StudentViewModel model) { Student student = new() { Id = model.Id, ClassId = model.ClassId, Number = model.Number, Name = model.Name, Gender = model.Gender, Birthday = model.Birthday, }; studentRepo.Update(student); } ``` # 创建post请求的Edit动作 和Add动作类似,只不过调用了Update方法,代码如下: ``` [HttpPost] public IActionResult Edit(StudentViewModel model) { if (ModelState.IsValid) { try { studentService.Update(model); return RedirectToAction("Index"); } catch (DbUpdateException) { ModelState.AddModelError(nameof(Student.Number), "学号不能重复"); } } if (model.ClassId == Guid.Empty) { ModelState.Remove(nameof(Student.ClassId)); ModelState.AddModelError(nameof(Student.ClassId), "请选择班级"); } StudentViewModel studentViewModel = studentService.GetSelectListModel(); model.Majors = studentViewModel.Majors; model.Grades = studentViewModel.Grades; return View(model); } ```
编辑学生