MVC分页让你更爽
创建Controller控件器与强类型视图呢,还有Models里面的对象实体,相信你们看了我之前发的就能创建好了,在这里呢就不创建了哈亲
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、网站建设、六合网络推广、小程序制作、六合网络营销、六合企业策划、六合品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供六合建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
在用户控件器里面写代码了
下面这个分页的方法
- ///
- /// 用户信息分页
- ///
- ///
- public ActionResult UserDataPage()
- {
- EFFristModelEntities ef = new EFFristModelEntities();
- int pageIndex = 1;//记录页的条数
- int pageSize = 3;//每页显示的条数
- int.TryParse(Request.QueryString["pageIndex"],out pageIndex);//强转一下这个记录页的条数
- int pageIndexCount =Convert.ToInt32(ef.UserInfo.Count());//获取 总的记录数
- int pageCount = Convert.ToInt32(Math.Ceiling(pageIndexCount*1.0/pageSize));//获取总的页数
- //判断页的取值范围
- if (pageIndex < 1)
- {
- pageIndex = 1;
- }
- if (pageIndex > pageCount)
- {
- pageIndex = pageCount;
- }
- //Linq语句
- var temp = from user in ef.UserInfo
- .OrderBy(user => user.ID)
- .Skip((pageIndex - 1) * pageSize)//跳过的序列
- .Take(pageSize)//返回指定数量的元素
- select user;
- ViewData.Model = temp;//把这个Linq语句读取的数据给这个实体model
- ViewData["pageIndex"] = pageIndex;
- ViewData["pageIndexCount"] = pageIndexCount;
- ViewData["pageCount"] = pageCount;
- ViewData["pageSize"] = pageSize;
- return View();
- }
这个在前台循环的展示数据
- <%for (int i = 1; i <= Convert.ToInt32(ViewData["pageCount"]); i++)
- { %>
- <%:Html.ActionLink(i.ToString(),"UserDataPage",new {pageIndex=i}) %>
- <%} %>
- <%:"当前页:" + ViewData["pageSize"] + "数据"%> <%:"共:"+ViewData["pageIndexCount"]+"条" %> <%:"共:"+ViewData["pageCount"]+"页" %>
第二种分页 有样式
这个是在models里面创建的类 是一个静态的类型为HtmlString的 是微软定义的一个类
- public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
- {
- var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
- pageSize = pageSize == 0 ? 3 : pageSize;
- var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
- var output = new StringBuilder();
- if (totalPages > 1)
- {
- //if (currentPage != 1)
- {//处理首页连接
- output.AppendFormat("首页 ", redirectTo, pageSize);
- }
- if (currentPage > 1)
- {//处理上一页的连接
- output.AppendFormat("上一页 ", redirectTo, currentPage - 1, pageSize);
- }
- else
- {
- // output.Append("上一页");
- }
- output.Append(" ");
- int currint = 5;
- for (int i = 0; i <= 10; i++)
- {//一共最多显示10个页码,前面5个,后面5个
- if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
- {
- if (currint == i)
- {//当前页处理
- //output.Append(string.Format("[{0}]", currentPage));
- output.AppendFormat("{3} ", redirectTo, currentPage, pageSize, currentPage);
- }
- else
- {//一般页处理
- output.AppendFormat("{3} ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
- }
- }
- output.Append(" ");
- }
- if (currentPage < totalPages)
- {//处理下一页的链接
- output.AppendFormat("下一页 ", redirectTo, currentPage + 1, pageSize);
- }
- else
- {
- //output.Append("下一页");
- }
- output.Append(" ");
- if (currentPage != totalPages)
- {
- output.AppendFormat("末页 ", redirectTo, totalPages, pageSize);
- }
- output.Append(" ");
- }
- output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行
- return new HtmlString(output.ToString());
- }
- 在前台调用这个方法即可
- <%:Html.ShowPageNavigate(Convert.ToInt32(ViewData["pageIndex"]),3,Convert.ToInt32(ViewData["pageCount"])) %>
这个是样式文件夹 这个呢可以在网上扒的哈,我这不一定你就能用的哦 亲
- .paginator
- {
- font: 12px Arial, Helvetica, sans-serif;
- padding: 10px 20px 10px 0;
- margin: 0px;
- }
- .paginator a
- {
- border: solid 1px #ccc;
- color: #0063dc;
- cursor: pointer;
- text-decoration: none;
- }
- .paginator a:visited
- {
- padding: 1px 6px;
- border: solid 1px #ddd;
- background: #fff;
- text-decoration: none;
- }
- .paginator .cpb
- {
- border: 1px solid #F50;
- font-weight: 700;
- color: #F50;
- background-color: #ffeee5;
- }
- .paginator a:hover
- {
- border: solid 1px #F50;
- color: #f60;
- text-decoration: none;
- }
- .paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover
- {
- float: left;
- height: 16px;
- line-height: 16px;
- min-width: 10px;
- _width: 10px;
- margin-right: 5px;
- text-align: center;
- white-space: nowrap;
- font-size: 12px;
- font-family: Arial,SimSun;
- padding: 0 3px;
- }
- /*****************************2*/
- .page_nav,
- #commentpage{clear:both; padding:15px 0; color:#666; font:normal 12px/24px Arial; text-align:center;}
- .page_nav a,
- #commentpage a{display:inline-block; height:22px; margin:0 2px; padding:0 8px; border:solid 1px #dbe5ee; -moz-border-radius:2px; -webkit-border-radius:2px; -khtml-border-radius:2px; border-radius:2px; background:#fff; color:#333; font:normal 12px/22px Arial, Helvetica, sans-serif; cursor:pointer;}
- .page_nav strong{display:inline-block; height:24px; margin:0 3px; padding:0 8px; border:none; -moz-border-radius:2px; -webkit-border-radius:2px; -khtml-border-radius:2px; border-radius:2px; background:#c00; color:#fff; font-weight:normal; line-height:24px; text-decoration:none;}
- .page_nav a:hover,
- .page_nav a.on{height:24px; margin:0 3px; border:none; background:#c00; color:#fff; line-height:24px; text-decoration:none;}
- .page_nav a.select{cursor:default;}
- .page_nav .view_all{display:block; text-align:center;}
- .page_nav .view_all a{height:auto; margin:0; padding:0; border:none; color:#06c; line-height:24px;}
- .page_nav .view_all a:hover{height:auto; margin:0; padding:0; background:none;}
网站栏目:MVC分页让你更爽
标题URL:http://scyingshan.cn/article/gehogo.html