Mvc分页

int pageIndex;
int pageSize = 9;
if(!int.TryParse(Request["pageIndex"],out pageIndex))
{
pageIndex = 1;
}

        mvcDbEntities db = new mvcDbEntities();
        //总页数
        int totalCount = db.YongHu.Count();
        int pageCount = Convert.ToInt32(Math.Ceiling(totalCount * 1.0 / pageSize));
        if (pageIndex < 1)
        {
            pageIndex = 1;
        }
        if(pageIndex>pageCount)
        {
            pageIndex = pageCount;
        }
        var yonghuList = db.YongHu.Where<YongHu>(c => true).OrderBy<YongHu, int>(c => c.YongHuId).Skip<YongHu>((pageIndex - 1) * pageSize).Take<YongHu>(pageSize);
        List<YongHu> list = yonghuList.ToList();
        ViewData.Model = list;
        ViewBag.PageIndex = pageIndex;
        ViewBag.PageSize = pageSize;
        ViewBag.PageCount = pageCount;
        ViewBag.TotalCount = totalCount;
        return View();

<%for(int i=1;i<=(int)ViewBag.PageCount;i++){ %>
<%:Html.ActionLink(i.ToString(),"Index",new {pageIndex=i}) %>
<%} %>

添加一个样式表

posted on 2015-03-19 21:48  木屐  阅读(100)  评论(0编辑  收藏  举报

导航