ABP进阶教程1 - 条件查询

点这里进入ABP进阶教程目录 

添加实体

打开领域层(即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

预览效果

 

 

 

posted @ 2019-10-09 17:38  智慧园区-老朱  阅读(2521)  评论(6编辑  收藏  举报