c# - 嵌套属性上的 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 。有没有其他方法可以做到这一点?
解决方案
推荐阅读
- azure - Azure 功能已部署但未运行
- php - 转换时间戳后获取错误日期
- asp.net-core-3.1 - 如何忽略特定枚举类型的全局 JsonStringEnumConverter
- javascript - React-router-dom 链接在我的项目上不起作用
- xcode - 如何隐藏iOS模拟器工具栏?
- oracle - 简单的 ODPI-C 语句执行示例给出 ORA-00922
- android - 在 Android Studio 中安装 Chaquo.python 失败
- winforms - 如何将 FluentDesignForm 与 RibbonControl 一起使用?
- python - 使用 Python 和 BeautifulSoup 从 Yahoo Finance 抓取股票价格
- python-3.x - 我无法用 pip 安装 cupy