MVC Contrib 网格在 .OrderBy() 上失败并出现 NHibernate 异常

发布于 2024-12-13 01:25:14 字数 1652 浏览 1 评论 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"} 访问视图时,它将

失败我该如何解决这个问题?

非常感谢,科汉。

I have been following this nice walkthrough for creating a grid using MVC contrib.

My version is however using NHibernate, not linq to entities.

The code is working fine up to the point of sorting.

    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);
    }

If i uncomment the line .OrderBy(gridSortOptions.Column, gridSortOptions.Direction)it will fail when it hits the view with an System.NotSupportedException {"NewExpression"}

Any ideas how i can solve this issue?

Many thanks, Kohan.

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

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

发布评论

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

评论(1

多情癖 2024-12-20 01:25:14

解决了......只需在应用过滤和排序后稍后转换视图模型即可。

  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);

Solved it... Simply had to cast the view model later on after the filtering and ordering had been applied.

  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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文