How paging works?

Aug 7, 2013 at 3:58 PM
Hi. Does this grid supports correct paging&

settings:
.WithPaging(2, 10, "page")


Problem:
2 - means records peer page
10 - total pages (2*10=20 records)

I do that query on request:

int totalPages;
var data = repository.GetRecords(pageNum, 2, out totalPages);

return View(new Model{Data=data, RecordsPerPage=2, TotalPages=totalPages });

Then in grid:
.WithPaging(Model.RecordsPerPage, Model.TotalPages, "page") and...

nothing

experiments show that grid expects ALL DATA. beacuse pager panel enable only you give more recors then Model.RecordsPerPage parametr.

I don't how you get 56 pages in http://gridmvc.azurewebsites.net/. It's seems that you take all database at once
Coordinator
Aug 7, 2013 at 4:12 PM
Hi,

Grid.Mvc expects that you pass all your data source collection. After that grid will cut only displaying page.

If your data source is database (for example) you need to pass IQueryable<T> collection to the grid.
Grid.Mvc uses IQueryable<T> interface to construct query expressions to your data collection.
When you go to some page the grid invokes .Skip(N).Take(N) methods and when you sort data the grid invokes OrderBy or OrderByDescending methods etc.
If you pass IQueryable collection produced by ORM (like Entity Framework) - the ORM will translate query to database (SQL queries). In this case Grid.Mvc query only one page of data, that currently displayed.
If you want to see how it works, you can download the sample app code from the repository.

PS: The second parameter of WithPaging method - is a max displaying page links of the pager, not total pages. The count of the items grid retrieve automatically.
Aug 7, 2013 at 6:26 PM
Thank you for your answer.

Two question:
  1. WithPaging(2, 10, "page") for what "page" parameter need then?
  2. Please, write how Grid.Mvc released database context (ORM/Linq-To-Sql) ? Does it safe to give it IQueryable?
Please, write somewhere on the project page that it takes IQueryable as model.
Coordinator
Aug 8, 2013 at 3:22 AM
The project uses EF and DbSet<T> provide IQueryable<T> out of box, see SqlRepository:
        public virtual IOrderedQueryable<T> GetAll()
        {
            return EfDbSet;
        }
  1. "page" - is the name of query string parameter, that should be used to switch pages. try to use simple: WithPaging(2)
  2. Grid.Mvc doesn't know about the data source, that uses in the app. It uses only IQueryable collection that you provide. There is no benefits of explicitly releasing your data context. If you are using DI container you can determine PreWebRequest scope for your data context, and it will automatically dispose it after web request ends.
Feb 7, 2014 at 11:02 AM
Edited Feb 7, 2014 at 11:05 AM
Hi,i am new to Grid.Mvc project.

I am passing a ((model).(List<my own entity class>)) to the grid [(List<EntityClassName> ListName=new [List<EntityClassName>) is in the model] as follows:

@Html.Grid(Model.ListName).Columns(columns=> { columns.Add(c=>c.ColumnName1);
.
.
.
So on...
}).WithPaging(5)
but i'm unable to do the paging properly.I have 9 rows in the List and i gave the size of Paging equals 5 i.e;i should get 5 records on 1st page and remaining 4 records on the 2nd page.But i m getting an empty page on click of 2,the href link as <a href="?deviceId=660&X-Requested-With=XMLHttpRequest&grid-page=2">2</a>...Actually i am selecting a deviceID based on which the the Model.ListName that i m passing to the grid will fetch the related info about that deviceID and render it on the webpage.i m getting the 1st page with 5 records but not the 2nd page.Please do post a solution if anyone is aware of the problem.
Feb 28, 2014 at 10:26 AM
Hi Bukharin,

Nice answer. But I am still having an issue of the simillar type. I have been using a Stored Procedure with Entity Framework to fetch my data.

There are thousands of records which I don't want to fetch all at a time. Also I can not use IQueryable, as the Stored Proc returns an ObjectResult.

Could you please suggest what should I do.

Thanks
Naresh
Coordinator
Mar 1, 2014 at 5:33 AM
Hi

There is no common solution for that, please see: https://gridmvc.codeplex.com/discussions/442422
Mar 4, 2014 at 4:50 AM
Hi Bukharin,

Thanks for your reply. I got the solution to my problem, rather than implementing the custom pager I converted my SP to UDF accepting parameters for where clause. This solves all of my problems as the return type of a UDF in Entity Framework is IQueryable. This is because a function returns some value which makes it possible to embed in other SQL statements just like the Tables.