ABP进阶教程3 - 优化编辑
我们尝试在新增/编辑界面增加一个下拉框用来代替输入框编辑Status
添加实体
打开领域层(即JD.CRS.Core)的Entitys目录 //用以存放实体对象
添加一个类StatusCode.cs //状态信息
using System; using System.Collections.Generic; using System.Text; namespace JD.CRS.Entitys { public enum StatusCode : byte { Enabled = 0, Disabled = 1 } }
更新模型
更新查询视图模型
打开展示层(即JD.CRS.Web.Mvc)的Models/Course/CourseListViewModel.cs //Course查询视图模型
新增GetStatusList方法 //获取状态列表
1 public StatusCode? Status { get; set; } 2 3 public List<SelectListItem> GetStatusList(ILocalizationManager localizationManager) 4 { 5 var list = new List<SelectListItem> 6 { 7 new SelectListItem 8 { 9 Text = localizationManager.GetString(CRSConsts.LocalizationSourceName, "PleaseSelect"), 10 Value = "", 11 Selected = Status == null 12 } 13 }; 14 15 list.AddRange(Enum.GetValues(typeof(StatusCode)) 16 .Cast<StatusCode>() 17 .Select(status => 18 new SelectListItem 19 { 20 Text = localizationManager.GetString(CRSConsts.LocalizationSourceName, $"StatusCode_{status}"), 21 Value = status.ToString(), 22 Selected = status == Status 23 }) 24 ); 25 26 return list; 27 }
更新编辑视图模型
打开展示层(即JD.CRS.Web.Mvc)的Models/Course/EditCourseModalViewModel.cs //Course编辑视图模型
新增GetStatusList方法 //获取状态列表
1 public List<SelectListItem> GetStatusList(ILocalizationManager localizationManager) 2 { 3 var list = new List<SelectListItem> 4 { 5 }; 6 7 list.AddRange(Enum.GetValues(typeof(StatusCode)) 8 .Cast<StatusCode>() 9 .Select(status => 10 new SelectListItem 11 { 12 Text = localizationManager.GetString(CRSConsts.LocalizationSourceName, $"StatusCode_{status}"), 13 Value = status.ToString(), 14 Selected = status == Status 15 }) 16 ); 17 18 return list; 19 }
更新控制器
打开展示层(即JD.CRS.Web.Mvc)的Controllers/CourseController.cs //Course控制器
更新EditCourseModal方法 //向EditCourseModalViewModel传参数Status
public async Task<ActionResult> EditCourseModal(int courseId) { var course = await _courseAppService.Get(new EntityDto<int>(courseId)); var model = new EditCourseModalViewModel { Course = course, Status = course.Status }; return View("_EditCourseModal", model); }
更新创建视图
打开展示层(即JD.CRS.Web.Mvc)的Views/Course/Index.cshtml //Course查询视图
添加下拉框Status //以替代输入框Status
<div class="row clearfix"> <div class="col-sm-12"> <div class="form-line"> <label class="form-label">@L("Status")</label> @Html.DropDownListFor( model => model.Status, Model.GetStatusList(LocalizationManager), new { @class = "form-control", id = "Status" }) @*<input name="Status" type="text" class="form-control" />*@ </div> </div> </div>
更新编辑视图
打开展示层(即JD.CRS.Web.Mvc)的Views/Course/_EditCourseModal.cshtml //Course编辑视图
添加下拉框Status //以替代输入框Status
<div class="row clearfix"> <div class="col-sm-12"> <div class="form-line"> <label class="form-label">@L("Status")</label> @Html.DropDownListFor( model => model.Status, Model.GetStatusList(LocalizationManager), new { @class = "form-control", id = "Status" }) @*<input name="Status" type="text" value="@Model.Course.Status" class="form-control" />*@ </div> </div> </div>
更新脚本
打开展示层(即JD.CRS.Web.Mvc)的\wwwroot\view-resources\Views\Course\_EditCourseModal.js //用以存放Course编辑相关脚本
在初始化Form时给下拉框传值
//Initial form _$modal.on('shown.bs.modal', function () { _$form.find('input[type=text]:first').focus();//focus first input _$status.val() = status;//Status value });
预览效果
创建课程
编辑课程