C# .Net MVC Razor 视图静态分页导航栏生成器
这里贴出3个重要组成部分
1.分页导航栏主算法代码(包括 @Html 扩展方法)
2.HTML 页面调用代码(包括导航栏样式)
3.Controller 控制器代码
4.测试路径:http://www.iqingcao.com/EBusiness/Paination/2?pc=10(这里只是个本机测试url实例,不同机子测试 域名部分(www.iqingcao.com)需要替换一下)
参数说明:pc(pageCount),总页数
其中的 2 是:pageIndex ,当前页索引;使用的是默认路由也就是 {controller}/{action}/{id} 形式的路由定义
5.效果展示
注:实现效果简单,如同学们有时间丰富一下配置参数,让其可设置性更强些。
丰富完了帖回来我也用哈
------------------------------------------------------------------------------------------------------------------------------
1.分页导航栏主算法代码
using System;
using System.Text;
using System.Web.Mvc;
namespace EB.Ctrl.Utility
{
#region Mvc 分页栏扩展方法
/// <summary>
/// Mvc 分页栏扩展方法
/// </summary>
public static class HtmlPaginationBar
{
/// <summary>
/// 生成分页栏(页面调用 @Html.PaginationBar)
/// </summary>
/// <param name="helper"></param>
/// <param name="bar">分页栏生成器</param>
/// <returns></returns>
public static MvcHtmlString PaginationBar(this HtmlHelper helper, PaginationBarBilder bar)
{
return new MvcHtmlString(bar.GenPaginationHtml());
}
}
#endregion
#region 分页条参数
/// <summary>
/// 分页条参数
/// </summary>
public class PageBarPars
{
/// <summary>
/// 总页数
/// </summary>
public int PageCount { set; get; }
/// <summary>
/// 当前页索引
/// </summary>
public int PageIndex { set; get; }
}
#endregion
#region 分页导航栏生成器
public class PaginationBarBilder
{
/// <summary>
/// 分页导航栏生成器
/// </summary>
/// <param name="url">页面地址模板,其中改变的页面索引部分使用{0}替换</param>
/// <param name="pageCount">总页数</param>
/// <param name="pageIndex">当前页索引</param>
public PaginationBarBilder(string url,int pageCount,int pageIndex)
{
m_url = url;
m_pageCount = pageCount;
m_pageIndex = pageIndex;
}
private int m_pageIndex = 0;
private int m_pageCount = 0;
private string m_url = string.Empty;
private int m_offset = 3;
private StringBuilder m_html = new StringBuilder();
/// <summary>
/// 生成分页m_html代码
/// </summary>
/// <returns></returns>
public string GenPaginationHtml()
{
StringBuilder m_html = new StringBuilder();
m_html.Append("<div id='pagination'>");
m_html.Append(GenPrevious());
m_html.Append(GenLeftAnchor());
m_html.Append(GenMidArea());
m_html.Append(GenRightAnchor());
m_html.Append(GenNext());
m_html.Append("</div>");
return m_html.ToString();
}
/// <summary>
/// 生成上一页
/// </summary>
private string GenPrevious()
{
StringBuilder m_html = new StringBuilder();
string href = string.Empty;
if (m_pageIndex <= 0 || m_pageCount<=1)
{
m_html.Append("<span class='current prev'>前一页</span>");
return m_html.ToString();
}
href = string.Format(m_url, m_pageIndex-1);
m_html.Append(
string.Format("<a class='prev' href='{0}'>前一页</a>", href));
return m_html.ToString();
}
/// <summary>
/// 生成后一页
/// </summary>
private string GenNext()
{
StringBuilder m_html = new StringBuilder();
string href = string.Empty;
if (m_pageIndex >= m_pageCount-1)
{
m_html.Append("<span class='current next'>后一页</span>");
return m_html.ToString();
}
href = string.Format(m_url, ++m_pageIndex);
m_html.Append(
string.Format("<a class='prev' href='{0}'>后一页</a>", href));
return m_html.ToString();
}
/// <summary>
/// 左停靠连接
/// </summary>
private string GenLeftAnchor()
{
StringBuilder m_html = new StringBuilder();
string href = string.Empty;
if (m_pageIndex - m_offset > 0)
{
href = string.Format(m_url, 0);
m_html.Append(string.Format("<a href='{0}'>1</a>", href));
}
if (m_pageIndex - m_offset > 1)
m_html.Append(string.Format("<span>...</span>"));
return m_html.ToString();
}
/// <summary>
/// 右停靠连接
/// </summary>
private string G
补充:软件开发 , C# ,