MvcContrib 对相关对象进行网格排序

发布于 2024-10-20 17:30:28 字数 390 浏览 3 评论 0原文

我想使用 MvcContrib Grid 对相关表进行服务器端排序

我有

 @Html.Grid(Model.Result).Columns(column =>
{
    column.For(u => u.Name).Named("Name");
    column.For(u => u.Country.Name).Named("Country").SortColumnName("Country");
 }).Sort(Model.GridSortOptions)

这可以正确显示表/网格。但是,我无法按 Country.Name 排序,出现错误

DbSortClause 表达式必须具有可比较顺序的类型

I want to do server side sorting of a related table using MvcContrib Grid

I have

 @Html.Grid(Model.Result).Columns(column =>
{
    column.For(u => u.Name).Named("Name");
    column.For(u => u.Country.Name).Named("Country").SortColumnName("Country");
 }).Sort(Model.GridSortOptions)

This displays the table/grid correctly. However I cannot sort by Country.Name I get an error

DbSortClause expressions must have a type that is order comparable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕梦 2024-10-27 17:30:28

做了一些挖掘,看起来 MvcContrib Grid 公开的本机 OrderBy() 方法不支持对子属性上的数据进行排序。

在处理数据呈现和排序的控制器操作中,您需要稍微自定义行为,而不是调用 data = data.OrderBy(sort.Column, sort.Direction) 。在您的情况下,最简单的解决方案可能是专门处理值“Country.Name”,然后对其余可排序列使用默认行为。像这样的东西应该足够了:

public ActionResult Index(GridSortOptions sort) {
  ViewData["sort"] = sort;
  var data = GetData();

  if (!string.IsNullOrEmpty(sort.Column)) {
    if(sort.Column.Equals("Country.Name", StringComparison.OrdinalIgnoreCase)) {
        if(sort.Direction == SortDirection.Ascending) {
            data = data.OrderBy(d => d.Country.Name);
        } else {
            data = data.OrderByDescending(d => d.Country.Name);
        }
    } else {
        data = data.OrderBy(sort.Column, sort.Direction);
    }   
  }

  return View(data);
}

I did some digging and it looks like the native OrderBy() method which MvcContrib Grid exposes does not support sorting data on a sub-property.

In the controller action which handles presenting and sorting the data, rather than calling data = data.OrderBy(sort.Column, sort.Direction), you will need to customize the behavior slightly. In your case, the easiest solution would likely be to handle the value "Country.Name" specially, and then use the default behavior for the rest of the sortable columns. Something like this should suffice:

public ActionResult Index(GridSortOptions sort) {
  ViewData["sort"] = sort;
  var data = GetData();

  if (!string.IsNullOrEmpty(sort.Column)) {
    if(sort.Column.Equals("Country.Name", StringComparison.OrdinalIgnoreCase)) {
        if(sort.Direction == SortDirection.Ascending) {
            data = data.OrderBy(d => d.Country.Name);
        } else {
            data = data.OrderByDescending(d => d.Country.Name);
        }
    } else {
        data = data.OrderBy(sort.Column, sort.Direction);
    }   
  }

  return View(data);
}
叫嚣ゝ 2024-10-27 17:30:28

我刚刚结束了一项工作。

我向 ViewModel 添加了一个名为“CountryName”的新属性,该属性映射到 Country.Name。这很好用。

所以我的代码现在

 @Html.Grid(Model.Result).Columns(column =>
{
    column.For(u => u.Name).Named("Name");
    column.For(u => u.CountryName).Named("Country");
 })

是我的 ViewModel

public string Name {get; set;}
public Country Country {get; set;}
public string CountryName {get; set;}

I just ended up doing a work around.

I added a new property to my ViewModel called "CountryName" which maps to Country.Name. This works fine.

So my code is now

 @Html.Grid(Model.Result).Columns(column =>
{
    column.For(u => u.Name).Named("Name");
    column.For(u => u.CountryName).Named("Country");
 })

And my ViewModel

public string Name {get; set;}
public Country Country {get; set;}
public string CountryName {get; set;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文