Sorting with MVCContrib

左心房为你撑大大i 提交于 2019-12-13 02:15:28

问题


Does anyone know how to sort the MVCContrib grid when using a complex object.

My grid is displaying a list of Person and I'm trying to sort on the Country property. The problem is that Country is a property an Address class which is a property of Person.

Person.Address.Country

    <%Html.Grid(Model).Columns(column =>
   {
       column.For(x => x.Id);
       column.For(x => x.FirstName);
       column.For(x => x.LastName).Sortable(false);
       column.For(x => x.Address.Country).Sortable(false);
       column.For(x => x.Age).Sortable(true);
   }).Render(); %>

Exception:
Property 'Country' is not defined for type '{Namespace}.Person'
var sourceProp = Expression.Property(sourceParam, this.SortBy); \MVCContrib\UI\Grid\Sortable\ComparableSortList.cs Line: 41

Any suggestions would be helpful.

Thank you,

MG1


回答1:


A workaround would be to expose Country as a property on Person and use that:

public string Country { get { return Address.Country; } }



回答2:


@orip gave you an answer.

But if you want to use the sorting feature you need to use:

<%Html.Grid(Model).Columns(column =>
{
   column.For(x => x.Id);
   column.For(x => x.FirstName);
   column.For(x => x.LastName).Sortable(false);
   column.For(x => x.Address.Country).Sortable(false);
   column.For(x => x.Age).Sortable(true);
}).RenderUsing(new SortableHtmlTableGridRenderer<Person>())
 .Render(); %>

Source: http://www.jeremyskinner.co.uk/2009/02/23/rewriting-the-mvccontrib-grid-part-3-gridmodels-and-gridrenderers/




回答3:


You need to use SortColumnName for this.

column.For(x => x.Address.Country).SortColumnName("Address.Country");

I have tested this and it works like a charm :)

If you are not able to access SortColumnName(), you can get the latest version of MVC contrib from http://mvccontrib.codeplex.com/SourceControl/changeset/changes/7db1cecc938f



来源:https://stackoverflow.com/questions/1562804/sorting-with-mvccontrib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!