MVC Contrib网格在带有NHibernate异常的.OrderBy()上失败

时间:2011-10-31 11:11:34

标签: asp.net-mvc-3 nhibernate fluent-nhibernate mvccontrib-grid

我一直关注使用MVC contrib创建网格的this很好的演练。

然而,我的版本使用的是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"}

点击视图时会失败

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

非常感谢,Kohan。

1 个答案:

答案 0 :(得分: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);