@using System.Collections.Specialized;
@functions{
    //默认分页函数

    private string Pagebreak(int page, string urlPrefix, string urlParams = "")
    {
        string rv = "";
        if (page > 1)
        {
            rv = urlPrefix + "/" + page;
        }
        else
        {
            rv = urlPrefix;
        }
        rv += urlParams;
        return rv;
    }

    private string GetUrlParams()
    {
        string rv = "";
        NameValueCollection queryStrings = Request.QueryString;
        int count = queryStrings.Count;
        if (count > 0)
        {
            rv = "?";
        }
        foreach (string key in queryStrings)
        {
            if (rv.IndexOf("=") > 0)
            {
                rv += "&";
            }
            rv += key + "=" + HttpUtility.UrlEncode(queryStrings[key]);
        }
        return rv;
    }
}

@{
    Tuple<PageInfo, string> model = (Tuple<PageInfo, string>)Model;
    PageInfo pageInfo = model.Item1;
    int pageCount = pageInfo.PageCount;
    int pageSize = pageInfo.PageSize;
    int recordCount = pageInfo.RecordCount;
    int currentPage = pageInfo.CurrentPage;
    int m = 2; //左右两边显示页码数
    string urlParams = GetUrlParams();
    string urlPrefix = model.Item2;
}
@if (pageCount > 1)
{
    if (currentPage > 1)
    {
        <a href="@Pagebreak(currentPage-1,urlPrefix,urlParams)">上一页</a>
    }

    int startPage = currentPage - m;
    int lastPage = currentPage + m;
    int cha = 0;
    if (lastPage > pageCount)
    {
        cha = lastPage - pageCount;
        startPage = startPage - cha;
        if (startPage < 1)
        {
            startPage = 1;
        }
        lastPage = pageCount;
    }
    else if (startPage < 1)
    {
        cha = 1 - startPage;
        startPage = startPage + cha;
        lastPage = lastPage + cha;
        if (lastPage > pageCount)
        {
            lastPage = pageCount;
        }
    }

    if (startPage > 1)
    {
        <text>
            <a href="@Pagebreak(1,urlPrefix,urlParams)">1</a> ...
        </text>
    }
    for (int i = startPage; i <= lastPage; i++)
    {
        <a href="@Pagebreak(i,urlPrefix,urlParams)" @Html.Raw(currentPage == i ? "class=\"current\"" : "")>@i</a>
    }
    if (lastPage < pageCount)
    {
        <text>
            ...<a href="@Pagebreak(pageCount,urlPrefix,urlParams)" )>@pageCount</a>
        </text>
    }
    if (currentPage < pageCount)
    {
        <a href="@Pagebreak(currentPage+1,urlPrefix,urlParams)">下一页</a>
    }
}
