分页显示
DAL:
public List GetListByPager(int PageIndex, int PageSize, out int RowCount)
{
string sql = "select top " + PageSize + " * from GTMessage where GId not in (select top ((" + PageIndex + "-1)*" + PageSize + ") GId from GTMessage)";
string sqll = "select count(*) from GTMessage";
RowCount = Convert.ToInt32(DBHelper.ExecuteScalar(sqll));
DataTable dt = DBHelper.GetTable(sql);
List list = new List();
for (int i = 0; i < dt.Rows.Count; i++)
{
GModel g = new GModel();
g.GId = Convert.ToInt32(dt.Rows[i]["GId"]);
g.GCarId = dt.Rows[i]["GCarId"].ToString();
g.GHomeId = dt.Rows[i]["GHomeId"].ToString();
g.GGoods = dt.Rows[i]["GGoods"].ToString();
g.GTime = dt.Rows[i]["GTime"].ToString();
g.GName = dt.Rows[i]["GName"].ToString();
list.Add(g);
}
return list;
}
Controllers:
public ActionResult Index()
{
int PageIndex = 1;
int PageSize = 5;
int RowCount = 0;
int PageCount = 0;
if (Request["PageIndex"] != null)
{
PageIndex = Convert.ToInt32(Request["PageIndex"]);
}
if (PageIndex < 1)
{
PageIndex = 1;
}
if (Session["PageCount"] != null && PageIndex > Convert.ToInt32(Session["PageCount"]))
{
PageIndex = Convert.ToInt32(Session["PageCount"]);
}
ViewData["PIndex"] = PageIndex;
List<GModel> list = bll.GetListByPager(PageIndex, PageSize, out RowCount);
PageCount = RowCount % PageSize == 0 ? RowCount / PageSize : RowCount / PageSize + 1;
Session["PageCount"] = PageCount;
return View(list);
}
View:
<a href="/Home/Index/1">首页</a>
<a href="/Home/[email protected](Convert.ToInt32(ViewData["PIndex"])-1)">上一页</a>
@for (int ii = 0; ii < Convert.ToInt32(Session["PageCount"]); ii++)
{
if ((ii + 1) == Convert.ToInt32(ViewData["PIndex"]))
{
<a href="/Home/[email protected](ii+1)" style="font-size:20px;color:red">@(ii + 1)</a>
}
else
{
<a href="/Home/[email protected](ii+1)">@(ii + 1)</a>
}
}
<a href="/Home/[email protected](Convert.ToInt32(ViewData["PIndex"]) + 1)">下一页</a>
<a href="/Home/[email protected]["PageCount"]">尾页</a>