joeharrison714 / MVCGrid.Net

http://mvcgrid.net
MIT License
74 stars 55 forks source link

Sorting MVCGrid is not working #180

Open RafaelLiendo2409 opened 5 years ago

RafaelLiendo2409 commented 5 years ago

Hi

I am trying to use this, but is not working the sorting. This is my code, I enabled sorting in both the grid and columns. Is there an error?. I do not see any error in the console browser. It just do not sort.

Code:

ColumnDefaults colDefaults = new ColumnDefaults() { EnableSorting = true };

MVCGridDefinitionTable.Add("SitiosL", new MVCGridBuilder(colDefaults) .WithAuthorizationType(AuthorizationType.AllowAnonymous) .AddColumns(cols => { // Add your columns here cols.Add("ID").WithSorting(false) .WithValueExpression(i => i.ID.ToString()); // use the Value Expression to return the cell text for this column cols.Add("Sitio").WithSorting(true) .WithHeaderText("Sitio") .WithValueExpression(i => i.Sitio); cols.Add("TipoSitio").WithSorting(false) .WithHeaderText("Tipo de Sitio") .WithValueExpression(i => i.TipoSitio1.TipoSitio1);

})
.WithSorting(true,"Sitio")
.WithRetrieveDataMethod((context) =>
{
    var options = context.QueryOptions;
    var result = new QueryResult<Sitios>();
    using (var db = new SIGEMAEntities())
    {
        var query = db.Sitios.Include(s => s.TipoSitio1).ToList();

        if (!String.IsNullOrWhiteSpace(options.SortColumnName))
        {
            switch (options.SortColumnName.ToLower())
            {
                case "Sitio":

                    if (options.SortDirection == SortDirection.Asc)
                        query = query.OrderBy(c=>c.Sitio).ToList();
                    else query = query.OrderByDescending(c => c.Sitio).ToList();
                    break;

            }
        }

        result.Items = query;
    }

    return result;

})

);

econbassman commented 5 years ago

I know this is an old "issue", but this might help someone.

You are changing case to lower, here: switch (options.SortColumnName.ToLower()) BUT, you test using mixed case, here: case "Sitio":

Either drop the ToLower() function, or use lowercase in your case statement.