This project is read-only.

Applying SortOrder to Tracked DataGrid Items

Dec 13, 2012 at 1:48 AM
Edited Dec 13, 2012 at 12:34 PM

Hi, I am new to the MVC Controls Toolkit. I am using v2.3 within an ASP.NET 4.5 project.

When my test app hits the following line of controller class code on data grid column sorting postback:



I get this exception:

Object of type 'System.Func`2[Processor,System.String]' cannot be converted to type 'System.Func`2[MVCControlsToolkit.Controller.Tracker`1[Processor],System.String]'

I have sorting defined in my view as follows:

<%: Html.EnableSortingFor(m => m.Processors, m => m.SortOrder, "gridView-header", "gridView-headerAscending", "gridView-headerDescending", true, page: m => m.CurrentPage, oneColumnSorting: true) %>

Why does m.SortOrder contain a function pointer to a Processor instead of a pointer to a Tracker<Processor>? I am trying to apply a sort that came from the following data grid definition:

<%: Html.DataGridFor(m => m.Processors,, null, "DisplayItemTemplate", "GridTemplate", itemCss: "gridView-row", altItemCss: "gridView-alternateRow") %>

My Processor and ProcessorsModel classes are defined as follows:

    public class Processor
        public string Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Vendor { get; set; }
        public string Version { get; set; }
        public string Sku { get; set; }
        public string Created { get; set; }
        public string Modified { get; set; }

    public class ProcessorsModel
        public List<Tracker<Processor>> Processors { get; set; }
        public List<KeyValuePair<LambdaExpression, OrderType>> SortOrder { get; set; }
        public int PreviousPage { get; set; }
        public int CurrentPage { get; set; }
        public int TotalPages { get; set; }
        public int PageSize { get; set; }

Please let me know what I am missing. I would like to stick with the Tracker model and also implement in-place editing for other grid data. I don't need tracking for the Processors data since it is read-only. However, I don't see a NoTrack version of Html.DataGridFor.



Dec 13, 2012 at 7:29 PM

You have to apply the ordering BEFORE enclosing the Processor objects in a tracker object. You have done a formal error but also a conceptual one. In fact the ordering must be applied in the linq expression before the IQueryable is executed by retrieving the data from the database. Otherwise the ordering operation is performed in memory instead than in the database. 


Please refer to this tutorial and to its associted code to understand better how to use the ApplyOrder linq operator: 

Mvc Controls Toolkit Datagrid Updated Tutorial

Dec 14, 2012 at 7:24 PM

The Processor data source is not a database. It is a web service that does not support data sorting or paging. Therefore, the intent is to perform sorting and paging in server-side memory in the controller class. I reordered the controller data processing to apply sorting (via ApplyOrder) and paging before apply tracker wrappers and returning the data to the grid view. It all works now. Thanks for your help.