如何在MVC框架中使用HtmlHelper扩展类实现一个分页功能-创新互联
                                            这篇文章给大家介绍如何在MVC框架中使用HtmlHelper扩展类实现一个分页功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

具体内容如下
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace HtmlHelperMvc.Models
{
 /// 
 /// 分页类如果一个页面显示两个列表只需要复制该类到项目中重命名一个就可以
 ///  
 public static class PagingHelper
 {
  #region 属性Property
  /// 
  /// 当前页码
  ///  
  private static int? _currentPage = null;
  /// 
  /// 当前页码
  ///  
  public static int CurrentPage
  {
   get
   {
    return _currentPage ?? 1;
   }
   set
   {
    _currentPage = value;
   }
  }
  /// 
  /// 每页记录条数
  ///  
  private static int? _pageSize = null;
  /// 
  /// 每页记录条数
  ///  
  public static int PageSize
  {
   get
   {
    return _pageSize ?? 15;
   }
   set
   {
    _pageSize = value;
   }
  }
  /// 
  /// 是否显示上一页
  ///  
  public static bool HasPreviousPage
  {
   get
   {
    return (CurrentPage > 1);
   }
  }
  /// 
  /// 是否显示下一页
  ///  
  public static bool HasNextPage
  {
   get
   {
    return (CurrentPage < TotalPages);
   }
  }
  /// 
  /// 当前页:
  ///  
  public static string CurrentPageDisplayName { get; set; }
  /// 
  /// 每页显示:
  ///  
  public static string PageSizeDisplayName { get; set; }
  public static string FirstDisplayName { get; set; }
  public static string PreDisplayName { get; set; }
  public static string NextDisplayName { get; set; }
  public static string LastDisplayName { get; set; }
  public static string TotalCountDisplayName { get; set; }
  public static string TotalPagesDisplayName { get; set; }
  /// 
  /// 总条数
  ///  
  public static int TotalCount
  {
   get;
   set;
  }
  public static int TotalPages
  {
   get
   {
    return (int)Math.Ceiling(TotalCount / (double)PageSize);
    //return (TotalCount % PageSize == 0 ? TotalCount / PageSize : TotalCount / PageSize + 1);
   }
  }
  /// 
  /// 设置分页url eg:/Admin/Product/Index
  ///  
  public static string PagingUrl
  {
   get;
   set;
  }
  /// 
  /// 默认page,设置分页参数名 eg:/Admin/Product/Index?PagingParamName=1
  ///  
  public static string PagingParamName
  {
   get;
   set;
  }
  #endregion
  #region Paging String
  /// 
  /// MVC分页 如果用jquery分页只需要class不需要href,用以下实现:
  /// $(".class值").live("click", function () {
  /// var page = $(this).attr("pagingParamName值");
  /// $("#order").html("").load("/Customer/Order?page="+page);
  /// });live自动给遍历增加事件
  ///  
  /// 
  /// new {@class="grey",pagingParamName="page",href="/Admin/Product/Index" rel="external nofollow" } pagingParamName默认page,匿名类添加控件属性
  /// 后台Controller代码
//
// GET: /Home/
public ActionResult Index(int? page)
{
 page = page ?? 1;
 PagingHelper.CurrentPage = Convert.ToInt32(page);
 PagingHelper.PageSize = 20;
 //{获取数据集的中条数,以及分页的数据集}
 PagingHelper.TotalCount = 2000;
 return View();
}前台页面代码
@{
 ViewBag.Title = "Index";
}
@using HtmlHelperMvc.Models;
Index
关于如何在MVC框架中使用HtmlHelper扩展类实现一个分页功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
文章题目:如何在MVC框架中使用HtmlHelper扩展类实现一个分页功能-创新互联
地址分享:http://www.scyingshan.cn/article/cshhde.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 