點這裡進入ABP進階教程目錄 添加實體 打開領域層(即JD.CRS.Core)的Entitys目錄 //用以存放實體對象添加一個類StatusCode.cs //狀態信息 1 using System; 2 using System.Collections.Generic; 3 using Syst ...
添加實體
打開領域層(即JD.CRS.Core)的Entitys目錄 //用以存放實體對象
添加一個類StatusCode.cs //狀態信息
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace JD.CRS.Entitys 6 { 7 public enum StatusCode : byte 8 { 9 Enabled = 0, 10 Disabled = 1 11 } 12 }View Code
添加數據傳輸對象
打開應用層(即JD.CRS.Application)的Course\Dto目錄 //用以存放Course相關數據傳輸對象
添加一個Dto GetAllCoursesInput.cs
1 using Abp.Application.Services.Dto; 2 using JD.CRS.Entitys; 3 4 namespace JD.CRS.Course.Dto 5 { 6 public class GetAllCoursesInput: PagedResultRequestDto 7 { 8 public StatusCode? Status { get; set; } 9 } 10 }View Code
更新應用服務
打開應用層(即JD.CRS.Application)的Course\CourseAppService.cs //Course應用服務
重寫GetAll方法 //按狀態查詢Course
1 public override async Task<PagedResultDto<CourseDto>> GetAll(GetAllCoursesInput input) 2 { 3 var query = base.CreateFilteredQuery(input).WhereIf(input.Status.HasValue, t => t.Status == input.Status.Value); 4 var coursecount = query.Count(); 5 var courselist = query.ToList(); 6 return new PagedResultDto<CourseDto>() 7 { 8 TotalCount = coursecount, 9 Items = ObjectMapper.Map<List<CourseDto>>(courselist) 10 }; 11 }View Code
更新模型
打開展示層(即JD.CRS.Web.Mvc)的Models/Course/CourseListViewModel.cs //Course查詢視圖模型
新增GetStatusList方法 //獲取狀態列表
1 public StatusCode? SelectedStatus { 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, "All"), 10 Value = "", 11 Selected = SelectedStatus == 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 == SelectedStatus 23 }) 24 );View Code
更新控制器
打開展示層(即JD.CRS.Web.Mvc)的Controllers/CourseController.cs //Course控制器
更新Index方法 //按狀態查詢Course
1 public async Task<ActionResult> Index(GetAllCoursesInput input) 2 { 3 IReadOnlyList<CourseDto> output = (await _courseAppService.GetAll(new GetAllCoursesInput { Status = input.Status })).Items; 4 var model = new CourseListViewModel(output) 5 { 6 SelectedStatus = input.Status 7 }; 8 return View(model); 9 }View Code
更新視圖
打開展示層(即JD.CRS.Web.Mvc)的Views/Course/Index.cshtml //Course查詢視圖
插入查詢條件 //按狀態查詢Course
1 <div class="header"> 2 <table> 3 <thead> 4 <tr> 5 <th class="col-sm-1">@L("Status")</th> 6 <th class="col-sm-1"> 7 @Html.DropDownListFor( 8 model => model.SelectedStatus, 9 Model.GetStatusList(LocalizationManager), 10 new 11 { 12 @class = "form-control", 13 id = "StatusCombobox" 14 }) 15 </th> 16 <th class="col-sm-10"></th> 17 </tr> 18 </thead> 19 </table> 20 </div>View Code
更新腳本
打開展示層(即JD.CRS.Web.Mvc)的\wwwroot\view-resources\Views\Course\Index.js //用以存放Course查詢相關腳本
插入查詢條件 //按狀態查詢Course
1 var _$statusCombobox = $('#StatusCombobox'); 2 _$statusCombobox.change(function () { 3 location.href = '/Course?status=' + _$statusCombobox.val(); 4 });View Code
預覽效果