MVC实现自定义分页

概述:本方法可以实现对用List作为数据媒介的项目分页


运行结果如图

MVC实现自定义分页_第1张图片


分页帮助类

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ServicePlatform.Lib
{
    public static class PageHelper
    {
       

        public static List GetPage(List list, int perCount, int pageIndex)
        {
            
            int startIndex=perCount * (pageIndex - 1);
            //是否越界
            if (startIndex + perCount-1< list.Count)
            {
                return list.GetRange(startIndex, perCount);
            }
            else
            {
                //是否因为是最后一页而越界               
                if ( startIndex< list.Count)
                {
                    return list.GetRange(startIndex, list.Count - startIndex );
                }
                else
                {
                    return new List();
                }
                
            }
           
        }
    }
}

控制器调用

public ActionResult ExpertListShow(int perCount=8, int pageIndex=1)
         {
             List ExpertList = db.Expert.ToList();//获取专家
           
             //分页相关开始
             ViewBag.link = "/Business/Home/ExpertListShow";
             ViewBag.total = ExpertList.Count;
             ViewBag.currentPage = pageIndex;
             ViewBag.perCount = perCount;
             ViewBag.ExpertList = ServicePlatform.Lib.PageHelper.GetPage(ExpertList, perCount, pageIndex);
             //分页相关结束

             //ViewBag.ExpertList = ExpertList;
            
             return View();
         }

主视图


        @Html.Partial("_CutPage")
 

分部视图  _CutPage

共 @(ViewBag.total % ViewBag.perCount == 0 ? ViewBag.total / ViewBag.perCount : ViewBag.total / ViewBag.perCount + 1) 页
每页显示 跳转到第


0积分下载封装好的dll  点击下载


你可能感兴趣的:(C#,Mvc4)