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> 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()
 7     {
 8         TotalCount = coursecount,
 9         Items = ObjectMapper.Map>(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 GetStatusList(ILocalizationManager localizationManager)
 4 {
 5     var list = new List
 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()
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 Index(GetAllCoursesInput input)
2 {
3     IReadOnlyList 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

预览效果

ABP进阶教程1 - 条件查询_第1张图片

 

 ABP进阶教程1 - 条件查询_第2张图片

 

你可能感兴趣的:(ABP进阶教程1 - 条件查询)