joeharrison714 / MVCGrid.Net

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

Page size not display 10 per page items. #132

Closed karthikking closed 7 years ago

karthikking commented 7 years ago

My code

I have issue is page list of records per page. I have overall records as 17 in list and page size mentioned as 10, not listed 10 records in per page, it is listed 17 records single page but footer items records and page are showing as 10 per page. Issue in listed items in grid data.

My code

public static void RegisterGrids() { // add your Grid definitions here, using the MVCGridDefinitionTable.Add method GridDefaults defaultSet1 = new GridDefaults() { Paging = true, ItemsPerPage = 10, DefaultSortColumn = "Org_Street", MaxItemsPerPage = 10, Sorting = true, NoResultsMessage = "Sorry, no results were found"

        };

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

        MVCGridDefinitionTable.Add("EmployeeGrid", new MVCGridBuilder<TVSC_Entities.Organization>(defaultSet1, colDefaults)
.WithAuthorizationType(AuthorizationType.AllowAnonymous)
.AddColumns(cols =>
{
    cols.Add("Org_ID").WithSorting(false)
        .WithValueExpression(p => p.Org_ID.ToString());
    cols.Add("Organization").WithSorting(true).WithFiltering(true).WithHeaderText("Organization")
        .WithValueExpression(p => p.Org_Name);
    cols.Add("Street").WithHeaderText("Street")
        .WithValueExpression(p => p.Org_Street);
})
.WithSorting(true, "Org_Street")
.WithPaging(true, 10)
.WithRetrieveDataMethod((context) =>
{
    var options = context.QueryOptions;

    var result = new QueryResult<TVSC_Entities.Organization>();

    using (var db = new TVSC_DBContext())
    {

        var orglist = db.Organizations;
        var query = from s in orglist
                    orderby s.Org_ID, s.Org_Name, s.Org_Street
                    select s;

        //var query = db.Organizations.AsQueryable().OrderBy(p => p.Org_Name);
        //var query = db.Organizations.Where(p => Convert.ToBoolean(p.Org_Name));
        //var query = db.Organizations.ToList();
        ////var orgs = db.Organizations.ToList();
        result.TotalRecords = query.Count();
        if (!String.IsNullOrWhiteSpace(options.SortColumnName))
        {
            switch (options.SortColumnName.ToLower())
            {
                case "organization":
                    query = options.SortDirection == SortDirection.Asc ? query.OrderBy(c => c.Org_Name) :
                         query.OrderByDescending(c => c.Org_Name);
                    //else if (options.SortDirection == SortDirection.Dsc)
                    //    query = query.OrderByDescending(c => c.Org_Name);
                    //var direction = System.ComponentModel.ListSortDirection.Ascending;
                    //query = query.OrderBy(p => p.Org_Name, direction);
                    //query = query.OrderByDescending(x => x.Org_Name);
                    break;
                case "street":
                    query = options.SortDirection == SortDirection.Asc ? query.OrderBy(c => c.Org_Street) :
                        query.OrderByDescending(c => c.Org_Street);

                    //if (options.SortDirection == SortDirection.Asc)
                    //    query = query.OrderBy(c => c.Org_Street);
                    //else if (options.SortDirection == SortDirection.Dsc)
                    //    query = query.OrderByDescending(c => c.Org_Street);
                    break;
            }
        }
        if (options.GetLimitOffset().HasValue)
        {
            //query =query.Skip(options.GetLimitOffset().Value).Take(options.GetLimitRowcount().Value);

        }
        result.Items = query.ToList();
        result.TotalRecords = query.Count();
    }
    return result;
})

Thanks Kaarthik.

zairja commented 7 years ago

You're setting result.TotalRecords twice: the first time is correct (before checking the sort column), the second time is incorrect since it will get the count after the query has been told to limit the entries to 10 (or whatever limit). On the other hand, it's hard to tell what you're doing and what you want to accomplish, since many lines are commented out (including the one that gets the paged results [query.Skip]).

karthikking commented 7 years ago

Thanks i have fixed result.TotalRecords issue