首页 > 解决方案 > 嵌套属性上的 PagingList 排序

问题描述

假设我有两个 ViewModel:TasksViewModel.cs

public class TaskViewModel
{
    public String Title {get; set;}
    public DepartmentViewModel Department {get; set;}
}

和 DeprtmentViewModel.cs

public class DeprtmentViewModel
{
    public String Title {get; set;}
}

我正在创建 TaskViewModel 的 PagingList 并将其显示在 Razor 页面的表上。

控制器中的 PagingList 代码:

PagingList.Create<TaskViewModel>(await _taskService.GetAll(taskFilterViewModel), _appSettings.PageSize, page,sortExpression,sortExpression);

我想让用户能够根据任务标题和部门标题对表格进行排序。

我能够对任务标题实施排序,但我无法在部门标题(这是一个嵌套属性)上执行此操作。如何对嵌套属性的 PagingList 数据进行排序?

在 razor 页面的表头中:

<th>
    @Html.SortableHeaderFor(model => model.Title)
</th>
<th>
    @Html.SortableHeaderFor(model => model.Department.Title)
</th>

尝试通过单击部门标题对数据进行排序会给我一个 ArgumentException。

克服这个问题的一种方法是使用

@Html.SortableHeaderFor(model => model.Department)

在表头中并IComparable针对 DepartmentViewModel 的 Title 实现。是否有任何替代,因为,否则我必须为每个要实现排序的嵌套属性 Impelement IComparable 。有没有其他方法可以做到这一点?

标签: c#asp.netasp.net-core

解决方案


推荐阅读