Question

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.

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top