MVC Contrib 网格在 .OrderBy() 上失败并出现 NHibernate 异常
我一直在关注 这个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了......只需在应用过滤和排序后稍后转换视图模型即可。
Solved it... Simply had to cast the view model later on after the filtering and ordering had been applied.