//实现层
---------------------------------------------------------分割线---------------------------------------------------------
public List<UserModel> ShowListPage(int pageindex, int pagesize)
{
string sql = string.Format("select top({0}) *from (select ROW_NUMBER() over (order by Id)iid,*from UserType)fy where iid>(({1}-1)*{2})", pagesize, pageindex, pagesize);
DataTable dt = DBHelper.GetTable(sql);
var str = JsonConvert.SerializeObject(dt);
return JsonConvert.DeserializeObject<List<UserModel>>(str);
}
//控制器
---------------------------------------------------------分割线---------------------------------------------------------
public ActionResult ShowPage(int pageindex=1)
{
//一页显示2条数据
int pagesize = 2;
//记录总的条数
int pagecount = idal.ShowCount();
//当条数为总数时
if (pagecount % pagecount == 0)
{
ViewBag.fy = pagecount / pagesize;
}
else
{
ViewBag.fy = (pagecount / pagesize) + 1;
}
//页数
ViewBag.pageindex = pageindex;
ViewBag.pagesize = pagesize;
ViewBag.pagecount = pagecount;
ViewBag.list = idal.ShowListPage(pageindex, pagesize);
var resulr = idal.ShowCount();
return View(resulr);
}
//UI前台显示
---------------------------------------------------------分割线---------------------------------------------------------
@{
var shang = ViewBag.pageindex;
shang--;
if (shang < 0) { shang = 1; };
{
<input id="Button1" type="button" value="首页" onclick="location.href='/User/ShowPage?pageindex=1'" />
<input id="Button1" type="button" value="上一页" onclick="location.href='/User/ShowPage?pageindex=@shang'" />
}
}
@{
var next = ViewBag.pageindex;
next++;
if (next > ViewBag.fy) { next = ViewBag.fy; };
{
<input id="Button1" type="button" value="下一页" onclick="location.href='/User/ShowPage?pageindex=@next'" />
<input id="Button1" type="button" value="尾页" onclick="location.href='/User/ShowPage?pageindex=@ViewBag.fy'" />
}
}
SQL语句
---------------------------------------------------------分割线---------------------------------------------------------
--高老师传授的分页,毕生受用!!
select top(2) *from (select ROW_NUMBER() over (order by Id)iid,*from Exam02)fy where iid>((1-1)*2)