0

我一直在按照这个很好的演练使用 MVC contrib 创建网格。

然而,我的版本使用 NHibernate,而不是 linq 到实体。

代码在排序之前运行良好。

    public ActionResult Index(string clientName, int? countryId, GridSortOptions gridSortOptions, int? page)
    {
        var clientList = from c in this.ClientRepository.Query
                         select new ListedClientViewModel(c);

        //Set default sort column
        if (string.IsNullOrWhiteSpace(gridSortOptions.Column))
            gridSortOptions.Column = "ClientName";

        // Filter on clientName
        if (!string.IsNullOrWhiteSpace(clientName))
            clientList = clientList.Where(c => c.ClientName.StartsWith(clientName));

        // Filter on country
        if (countryId.HasValue)
            clientList = clientList.Where(c => c.CountryId == countryId);

        // Order and page the clients
        var clientPageList = clientList
            //Sorting causes error.
            //.OrderBy(gridSortOptions.Column, gridSortOptions.Direction)
            .AsPagination(page ?? 1, 10);

        var clientListContainer = new ClientListContainerViewModel
        {
            ClientPageList = clientPageList,
            GridSortOptions = gridSortOptions
        };

        return View(clientListContainer);
    }

如果我取消注释该行.OrderBy(gridSortOptions.Column, gridSortOptions.Direction),它会在它到达视图时失败System.NotSupportedException {"NewExpression"}

有什么想法可以解决这个问题吗?

非常感谢,科汉。

4

1 回答 1

0

解决了它......只需在应用过滤和排序后稍后投射视图模型。

  var clientList = this.ClientRepository.Query;

  ...
  ...

  // Order and page the clients
  var clientPageList = clientList
       //Sorting no longer causes error.
       .OrderBy(gridSortOptions.Column, gridSortOptions.Direction)
       .Select(c => new ListedClientViewModel(c))
       .AsPagination(page ?? 1, 10);
于 2011-10-31T15:00:07.977 回答