分页

//DAL层

public List<studentModel> show(string name = "",int index=1,int size=2)
{
int start = (index - 1) * size + 1;
int end = start + size - 1;
string sql = string.Format("select * from (select *,ROW_NUMBER()over(order by id)px from student where name like '%{0}%')t where t.px between {1}and {2}",name,start,end);
var n = db.GetDataTable(sql);
var list = JsonConvert.SerializeObject(n);
var table = JsonConvert.DeserializeObject<List<studentModel>>(list);
return table;
}
public int count()
{
string sq = "select count(id) from student";
var i = db.ExecuteScalar(sq);
return i;
}

//控制器
public ActionResult Index(string name = "",int index=1,int size=2)
{
ViewBag.index = index;
int count = st.count();
ViewBag.count = Math.Ceiling(count * 1.0 / size);
var n = st.show(name,index,size);
return View(n);
}

////显示视图

<input id="index" type="hidden" value="@ViewBag.index" />
<input id="count" type="hidden" value="@ViewBag.count" />
<input id="Button1" type="button" value="首页" onclick="sy()" />
<input id="Button1" type="button" value="上一页" onclick="syy()" />
<input id="Button1" type="button" value="下一页" onclick="xyy()" />
<input id="Button1" type="button" value="尾页" onclick="wy()" />

<script>
var index = 1;
var size = 2;
function sy() {
location.href = "/student/Index?index=1&size=2";
}
function syy() {
index = $("#index").val();
index <= 1 ? 1 : index--;
location.href = "/student/Index?index=" + index + "&size=" + size + "";
}
function xyy() {
index = $("#index").val();
var count = $("#count").val();
index >= count ? count : index++;
location.href = "/student/Index?index=" + index + "&size=" + size + "";
}
function wy() {
index = $("#count").val();
location.href = "/student/Index?index=" + index + "&size=" + size + "";
}
</script>

原文地址:https://www.cnblogs.com/gaoyuhui/p/10957298.html