代码改变世界

客户端实现分页

2019-03-18 14:00  .net小跟班(杜)  阅读(453)  评论(0编辑  收藏  举报

1、控制器

public ActionResult Index(int Pageindex = 1, int Pagesize = 3)
        {
            ViewBag.pageindex = Pageindex;
            string result = HttpClientHelper.Send("get", "/api/Emp/Get", null);
            List<EmpViewModel> list
                = JsonConvert.DeserializeObject<List<EmpViewModel>>(result);
            ViewBag.pagecount = Math.Ceiling(list.Count() * 1.0 / Pagesize);
            return View(list.Skip((Pageindex - 1) * Pagesize).Take(Pagesize));
        }

2、视图

<a href="/Emp/index?pageindex=@(ViewBag.pageindex-1)">上一页</a>
<a href="/Emp/index?pageindex=@(ViewBag.pageindex+1)">下一页</a>
<span>每页</span>
@Html.DropDownList("pagesize", new List<SelectListItem> {
new SelectListItem{ Text="1",Value="1"},
new SelectListItem{ Text="2",Value="2"},
new SelectListItem{ Text="3",Value="3", Selected = true},
new SelectListItem{ Text="4",Value="4"}
})
<span>条</span>
<script>
    $('#pagesize').change(function () {
        var pagesize=$(this).val();
        location.href = "/Emp/index?pageindex="+@ViewBag.pageindex+ "&pagesize=" + pagesize;
    })
</script>