今天写了分页,感觉有点难度,搞来搞去,最终还是解决了。我用的是调用存储过程的分页,
这个的难度主要是在于存储过程的编写,说实在的其实我也不太会,感觉上很朦胧的。
下面是用EF调用存储过程的方法:
public List<GetStaff_Result> listStaff(int pageSize, int pageIndex, int DepId, string SearchName, DateTime Date_temp, ref int pageCount)
{
List<GetStaff_Result> list = new List<GetStaff_Result>();
using (CustomerSysEntities2 cus = new CustomerSysEntities2())
{
//将内容存储到list中
list = cus.GetStaff(pageSize, pageIndex, DepId, SearchName, Date_temp).ToList();
}
pageCount = (int)list[0].resultcount;//总页数
//分配页数
if (pageCount % pageSize == 0)
{
pageCount = pageCount / pageSize;
return list;
}
else
{
pageCount = pageCount / pageSize + 1;
return list;
}
}
下面是Controller中相应的代码方法:
public ActionResult ManagerStaff(int pageIndex = 1, int DepId = 0, int pageCount=1, string SearchName = "")
{
//BindDepId();
Session["current"] = "员工管理";
DateTime Date_temp = new DateTime();
string time = "1990/1/1";
Date_temp = Convert.ToDateTime(time);
int pageSize = 4;
//int pageCount = 1;
List<GetStaff_Result> list = new List<GetStaff_Result>();
list=new StaffManager().listStaff(pageSize, pageIndex, DepId, SearchName, Date_temp, ref pageCount);
// 绑定下拉框
BindDepId();
// 向视图传递数据
ViewBag.list = list;
ViewBag.pageSize = pageSize;
ViewBag.pageCount = pageCount;
ViewBag.pageIndex = pageIndex;
//return View(list);
return View(list);
}
还有就是页面的传值以及页码的设置:
<div style="text-align: center">
@for (int i = 1; i <= ViewBag.pageCount; i++)
{
<a href="@Url.Action("ManagerStaff", "OASys", new { pageIndex=i,DepId=this.ViewContext.HttpContext.Request.QueryString.Get("DepId")??"0",SearchName=this.ViewContext.HttpContext.Request.QueryString.Get("SearchName")??"",Date_temp=this.ViewContext.HttpContext.Request.QueryString.Get("Date_temp")??""})">@i </a>
}
</div>
有问题的的可以再评论中说明,呵呵。