一路向前..

天下事有难易乎?为之,则难者亦易矣;不为,则易者亦难矣。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在项目用MVC开发也很久了,今天整理了下分页的方法,写个DEMO供大家交流参考下。

首先来看页面图片效果:

下面是项目文件截图:

 

在mvcweb项目中有个Global.asax.cs文件中有下代码:

routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Index", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

在PageController.cs中,我们用:

public ActionResult Index(string id)
        {
            return View(new Models.Page(id));
        }

红色字体部分名称必须要相同。

 

再来看Models目录下Page.cs的部分代码:

public Page(string para)
        {
            int pageSize = 10;
            PageParse sm = new PageParse(para, HttpContext.Current.Request.QueryString);
            int currentPage = StrToInt(sm["page"], 1);

            DataSource ds = new DataSource();//初始化数据源
            int totalCount = ds.objList.Count;//总记录数

            var q=ds.objList.AsQueryable<ObjectData>();
            if (currentPage > 0) q = q.Skip((currentPage - 1) * pageSize);

            aList = q.Take(pageSize).ToList();
            Paging = new Paging<ObjectData>(pageSize, currentPage, aList, totalCount, sm.PagingUrl);
        }

在aspx页面上,是这样来显示分页的:

<div style="border-bottom: 1px solid #D7D7D7;">
        <label>
            共<em><%=Model.Paging.TotalCount %></em>条记录</label>
        <div>
            <%=Model.Paging.GetStr() %>
        </div>
    </div>

至于MvcHelper项目,大家下载源码看看就明白。源码地址:https://files.cnblogs.com/howzanh/MvcPage.zip

posted on 2011-02-01 23:07  一路->向前  阅读(5443)  评论(13编辑  收藏  举报