Question

I'm pulling a paged dataset on an ASP.NET MVC3 application which uses JQuery to get data for endlesss scroll paging via $ajax call. The backend is a Azure SQL database. Here is the code:

[Authorize]
[OutputCache(Duration=0,NoStore=true)]
public PartialViewResult Search(int page = 1)
{ 

      int batch = 10; 
      int fromRecord = 1; 
      int toRecord = batch;

      if (page != 1)
      {
         //note these are correctly passed and set
         toRecord = (batch * page);
         fromRecord = (toRecord - (batch - 1));

      }

IQueryable<TheTable> query;

query = context.TheTable.Where(m => m.Username==HttpContext.User.Identity.Name)        
.OrderByDescending(d => d.CreatedOn)
.Skip(fromRecord).Take(toRecord);

//this should always be the batch size (10)
//but seems to concatenate the previous results ???

int count = query.ToList().Count();

//results
//call #1, count = 10
//call #2, count = 20
//call #3, count = 30
//etc...


PartialViewResult newPartialView = PartialView("Dashboard/_DataList", query.ToList());
return newPartialView;

 }

The data returned from each call from Jquery $ajax continues to GROW on each subsequent call rather then returning only 10 records per call. So the results return contain all of the earlier calls data as well. Also, I've added 'cache=false' to the $ajax call as well. Any ideas on what is going wrong here?

Was it helpful?

Solution

The values you're passing to Skip and Take are wrong.

  • The argument to Skip should be the number of records you want to skip, which should be 0 on the first page;

  • The argument to Take needs to be the number of records you want to return, which will always be equal to batch;

Your code needs to be:

int batch = 10; 
int fromRecord = 0; 
int toRecord = batch;

if (page != 1)
{
   fromRecord = batch * (page - 1);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top