0

我正在一个 ASP.NET MVC3 应用程序上提取分页数据集,该应用程序使用 JQuery 通过 $ajax 调用获取用于无限滚动分页的数据。后端是 Azure SQL 数据库。这是代码:

[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;

 }

从 Jquery $ajax 每次调用返回的数据在每次后续调用中继续增长,而不是每次调用仅返回 10 条记录。因此,返回的结果也包含所有早期调用数据。此外,我还在 $ajax 调用中添加了 'cache=false'。关于这里出了什么问题的任何想法?

4

1 回答 1

0

您传递给的值SkipTake错误的。

  • to 的参数Skip应该是要跳过的记录数,应该0在第一页;

  • to 的参数Take需要是您要返回的记录数,它始终等于batch;

您的代码必须是:

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

if (page != 1)
{
   fromRecord = batch * (page - 1);
}
于 2013-03-18T19:01:37.167 回答