我一直在按照这个很好的演练使用 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"}
有什么想法可以解决这个问题吗?
非常感谢,科汉。