joeharrison714 / MVCGrid.Net

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

Paging Not Working #80

Open krishnandusarkar opened 7 years ago

krishnandusarkar commented 7 years ago

Whenever I'm enabling Paging it displays There was a problem loading the grid.

If I turn off paging and use the normal grid, it works fine.

Am I doing something wrong?

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

            //Company Grid
            MVCGridDefinitionTable.Add("CompaniesGrid", new MVCGridBuilder<Company>(colDefaults)
                .WithAuthorizationType(AuthorizationType.AllowAnonymous)
                .AddColumns(cols =>
                {
                    // Add your columns here
                    cols.Add().WithColumnName("CompanyName")
                        .WithHeaderText("Company Name")
                        .WithValueExpression(i => i.CompanyName); // use the Value Expression to return the cell text for this column
                    cols.Add().WithColumnName("SubscriptionType")
                        .WithHeaderText("Subscription Type")
                        .WithValueExpression(i => i.SubscriptionType.ToString()); // use the Value Expression to return the cell text for this column
                    cols.Add().WithColumnName("ExpiryDate")
                        .WithHeaderText("Expiry Date")
                        .WithValueExpression(i => i.ExpiryDate.ToString("dd-MMM-yyyy hh:mm:ss")); // use the Value Expression to return the cell text for this column
                    cols.Add().WithColumnName("Details")
                        .WithSorting(false)
                        .WithHtmlEncoding(false)
                        .WithHeaderText("Details")
                        .WithValueExpression((i, c) => c.UrlHelper.Action("Details", "Companies", new { id = i.CompanyID }))
                        .WithValueTemplate("<a href='{Value}'>Details</a>"); ;
                })
                .WithSorting(true, "CompanyName")
                .WithPaging(true, 10)
                .WithRetrieveDataMethod((context) =>
                {
                    // Query your data here. Obey Ordering, paging and filtering parameters given in the context.QueryOptions.
                    // Use Entity Framework, a module from your IoC Container, or any other method.
                    // Return QueryResult object containing IEnumerable<YouModelItem>

                    var options = context.QueryOptions;
                    var result = new QueryResult<Company>();
                    using (var db = new ApplicationDbContext())
                    {
                        var query = db.Companies.AsQueryable();
                        result.TotalRecords = query.Count();
                        if (!String.IsNullOrWhiteSpace(options.SortColumnName))
                        {
                            switch (options.SortColumnName.ToLower())
                            {
                                case "companyname":
                                    switch (options.SortDirection)
                                    {
                                        case SortDirection.Asc:
                                            query = query.OrderBy(p => p.CompanyName);
                                            break;
                                        case SortDirection.Dsc:
                                            query = query.OrderByDescending(p => p.CompanyName);
                                            break;
                                    }
                                    break;
                                case "subscriptiontype":
                                    switch (options.SortDirection)
                                    {
                                        case SortDirection.Asc:
                                            query = query.OrderBy(p => p.SubscriptionType);
                                            break;
                                        case SortDirection.Dsc:
                                            query = query.OrderByDescending(p => p.SubscriptionType);
                                            break;
                                    }
                                    break;
                                case "expirydate":
                                    switch (options.SortDirection)
                                    {
                                        case SortDirection.Asc:
                                            query = query.OrderBy(p => p.ExpiryDate);
                                            break;
                                        case SortDirection.Dsc:
                                            query = query.OrderByDescending(p => p.ExpiryDate);
                                            break;
                                    }
                                    break;
                            }
                        }
                        if (options.GetLimitOffset().HasValue)
                        {
                            query = query.Skip(options.GetLimitOffset().Value).Take(options.GetLimitRowcount().Value);
                        }
                        result.Items = query.ToList();
                    }
                    return result;

                })
            );
joeharrison714 commented 7 years ago

Add the following appSetting to your web.config file to see the actual error details on grid generation instead of the custom error message: <add key="MVCGridShowErrorDetail" value="true" />