//實現層 分割線 public List<UserModel> ShowListPage(int pageindex, int pagesize) { string sql = string.Format("select top({0}) *from (select ROW_NUMBER() ov ...
//實現層
---------------------------------------------------------分割線---------------------------------------------------------
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/[email protected]'" />
}
}
SQL語句
---------------------------------------------------------分割線---------------------------------------------------------
--高老師傳授的分頁,畢生受用!!
select top(2) *from (select ROW_NUMBER() over (order by Id)iid,*from Exam02)fy where iid>((1-1)*2)