1
votes

Hope someone may be able to help. Trying to implement a jqGrid in ASP.NET MVC 4 C#. I keep getting the following error when trying to sort a column. This column is actually a field from an object using the foreign key. Not sure how the code should be changed to account for this. The grid sorts correctly for the fields that are in the model that I'm using, just not the field that is from the foreign key. Should I be using a ViewModel instead? I'm using the code from Oleg's examples. item.Name is the object and item.Name.FullName is the column I'm trying to sort that is throwing the error below. Thanks!

The ORDER BY sort key(s) type must be order-comparable. Near member access expression, line 6, column 3.

public JsonResult DynamicGridData(string sidx, string sord, int page, int rows, bool search, string filters)
    {
        var context = new TeamContext();
        var objectContext = ((IObjectContextAdapter)context).ObjectContext;
        var set = objectContext.CreateObjectSet<Team>();

        var serializer = new JavaScriptSerializer();

        Filters f = (!search || string.IsNullOrEmpty(filters)) ? null : serializer.Deserialize<Filters>(filters);

        ObjectQuery<Team> filteredQuery =
        (f == null ? (ObjectQuery<Team>)set : f.FilterObjectSet((ObjectQuery<Team>)set));

        filteredQuery.MergeOption = MergeOption.NoTracking; // we don't want to update the data

        var totalRecords = filteredQuery.Count();

        var pagedQuery = filteredQuery.Skip("it." + sidx + " " + sord, "@skip",
                                            new ObjectParameter("skip", (page - 1) * rows))
                                     .Top("@limit", new ObjectParameter("limit", rows));

        // to be able to use ToString() below which does NOT exist in the LINQ to Entity
        var queryDetails = (from item in pagedQuery
                            select new { item.TeamId, item.Year, item.Name }).ToList();

        return Json(new
        {
            total = (totalRecords + rows - 1) / rows,
            page,
            records = totalRecords,
            rows = (from item in queryDetails
                    select new[] {
                                    item.TeamId.ToString(),
                                    item.Year,
                                    item.Name.FullName
                                }).ToList()
        });
    }
1
Welcome to Stackoverflow! There's no need to prefix questions with "jqgrid". That's what tags are for. Please read meta.stackexchange.com/questions/19190 for the discussion if you're interested.Patrick

1 Answers

0
votes

This can fail for various reasons:

  • item.Name.FullName is a binary field in the database?
  • to access to the Name object through item, lazy loading has to be enabled. are you sure it is?

Anyway using a viewmodel with only data that you need is always a good idea, and is a good way to separate your data Access layer from your controller.