复杂对象上的 MvcContrib 网格排序 [英] MvcContrib Grid Sorting on complex object
问题描述
我正在尝试使用 MvcContrib
网格控件.但我似乎无法对包含其他对象的复杂对象进行排序.
I am trying to work with MvcContrib
Grid control. But I cannot seem to get the sorting to work on complex objects that hold other objects.
我已经在这个问题中设置了类似于 OP 的控制器/类/视图.使用 MVCContrib 排序
I have setup my controller/classes/Views similar to the OP in this question. Sorting with MVCContrib
我尝试将 SortColumnName 用于我的 childobject.property,但它给了我一个错误,说我的主对象没有这个属性.这是我的代码片段
I have tried to use the SortColumnName to my childobject.property but it gives me an error saying My main object does not have this property. This is my code snippet
//POCO类
class Issue {
public int ID {get; get; }
.....
public int priorityId {get; set;}
public virtual Priority priority {get; set;}
}
//控制器代码
public ViewResult Index(int? pageNo, GridSortOptions sort)
{
var issues = db.issues.Include(i => i.priority);
ViewBag.sort = sort;
if (!string.IsNullOrEmpty(sort.Column))
{
issues = issues.OrderBy(sort.Column, sort.Direction);
}
return View(issues.ToList().AsPagination(pageNo ?? 1, 10));
}
//查看Grid的代码
//View code for the Grid
@Html.Grid(Model).Sort(ViewBag.sort as GridSortOptions).Columns(column => {
column.For(issue => Html.ActionLink(" ", "Edit", new { id = issue.ID, areas = "Issues", controller = "Main"}, new { @id="editBtn"})).Named("Edit");
column.For(issue => Html.ActionLink(issue.ID.ToString(), "Edit", new {id = issue.ID, areas = "Issues", controller = "Main"})).Named("ID").Sortable(true);
column.For(issue => issue.priority.codeDesc).Named("Priority").SortColumnName("priority.codeDesc").Sortable(true);
}).Empty("No data found")
当我尝试对优先级字符串进行排序时,它给了我一个错误,说priority.codeDesc 不是问题的属性".
When I try to sort on the priority string, it gives me an error saying 'priority.codeDesc is not a property of Issue'.
TIA
推荐答案
这里的问题实际上与网格无关,而是与作为 MvcContrib 排序扩展的一部分提供的 .OrderBy 扩展方法有关.这个扩展相当简单,我只写它来涵盖您想要对对象的直接属性进行排序的简单情况,但是在您的情况下,您尝试对嵌套属性(priority.codeDesc")进行排序'不支持 - 你不能在这个扩展中使用点符号.
The issue here isn't actually related to the grid, but rather to the .OrderBy extension method provided as part of the MvcContrib sorting extensions. This extension is fairly simplistic and I only wrote it to cover simple cases where you want to sort on a direct property of the object, however in your case you're trying to order on a nested property ("priority.codeDesc") which isn't supported - you can't use dot notation with this extension.
您要么需要切换到使用不同的机制来执行实际排序,要么如果这是一次性的情况,那么您可以硬编码此特定列的排序逻辑(不理想,但如果它是a one off 那么它比编写一个新的排序机制更简单),例如:
You'd either need to switch to using a different mechanism to perform the actual sorting, or if this is a one-off situation then you could hard-code the sorting logic for this particular column (not ideal, but if it's a one off then it's simpler than writing a new sorting mechanism), eg:
if (!string.IsNullOrEmpty(sort.Column))
{
if(sort.Column == "priority.codeDesc")
{
issues = issues.OrderBy(x => x.priority.codeDesc);
}
else
{
issues = issues.OrderBy(sort.Column, sort.Direction);
}
}
这篇关于复杂对象上的 MvcContrib 网格排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!