我有两页数据,第二页只有一条数据,当我删除一条数据后,应该只有一页数据,page数应该也是一,但是 当我删除数据后,page数还是2,如下图
以后是我的代码
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Grid1_DeleteRows(JArray Grid1_fields, JArray selectedRows)
{
var grid1 = UIHelper.Grid("Grid1");
//数据源
var userInfoList = db.DepartmentCategory.AsNoTracking().ToList();
foreach (string rowId in selectedRows)
{
//删除数据
var userInfo = userInfoList.Where(r => r.Category == rowId).FirstOrDefault();
db.Entry(userInfo).State = EntityState.Deleted;
db.SaveChanges();
}
//从新检索数据源
var TypeClass = db.DepartmentCategory.AsNoTracking().ToList();
//总记录数
var recordCount = TypeClass.Count();
//分页数据
var dataSource = GetPagedDataSource(TypeClass, 1, recordCount);
//从新指定分页数
grid1.PageSize(15);
//绑定
grid1.DataSource(dataSource, Grid1_fields);
Alert.ShowInTop("删除数据成功!");
return UIHelper.Result();
}
private List<DepartmentCategory> GetPagedDataSource(List<DepartmentCategory> students,
int pageIndex, int recordCount)
{
var pageCount = GetPageCount(recordCount);
if (pageIndex >= pageCount && pageCount >= 1)
{
pageIndex = pageCount - 1;
}
return students.OrderBy(m => m.Category)
.Skip(pageIndex * PAGE_SIZE)
.Take(PAGE_SIZE).ToList();
}
private static readonly int PAGE_SIZE = 15;
private int GetPageCount(int recordCount)
{
int pageCount = recordCount / PAGE_SIZE;
if (recordCount % PAGE_SIZE != 0)
{
pageCount += 1;
}
return pageCount;
}
求大神帮帮忙。问题出在哪
|