asp.net mvc simple implementation based on razor paging control

Source: Internet
Author: User
Tags functions httpcontext key net string


I've been writing some web apps lately. ASP.net mvc found that it worked, so it went straight to the top. However, the use of the ASP.net MVC razor template does not allow the control to be used in the traditional webform. However, the function of razor has been replaced by the functions, but from the razor point of view is not what we usually say the control but the child template. Because Razor can split functionality into a child template according to its own needs, and then output through the Renderpage method. From a design standpoint, it is possible to encapsulate the paging control as a cshtml of a child, and then refer to it when needed.



The most important aspect of the design as a control is isolation and independence, and in this regard razor is also considered more thoughtful, through the pagedata can be simply the data coupling between the template problem. The following razor implements a simple paging control, in the implementation process only need to write a simple cshtml template. There is no need to refer to any code or DLL to use, but to embed related cshtml directly where needed can be the function of paging controls.


parameter Formulation


A control to output information must be related to the data specification, a simple paging control in fact only need two parameters that can, the current page index and total number of pages. You can sound at the top of the Razor template data specification


@{
int pageindex = (int) pagedata["pageindex"];
int spitindex = 0;
int pagecount = (int) pagedata["PageCount"];
}


These two parameters tell the user that they must take both parameter values when calling Renderpage input.


Paging URL Development


In some data query often in the URL will bring some relevant parameters, so in the input page URL of the time need to deal with a simple.


 @functions{
    public static string GetUrl(int index)
    {
        System.Collections.Specialized.NameValueCollection querystring = HttpContext.Current.Request.QueryString;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("?").AppendFormat("pageindex=" + index);
        foreach(string key in querystring.Keys)
        {
            if (key != "pageindex")
                sb.AppendFormat("&{0}={1}", key, HttpContext.Current.Server.UrlEncode(querystring[key]));
        }
        return sb.ToString();
    }
}

The

Simply attaches the pageindex to the current URL querystring. The
paging content output

has the above information that building a paging content output is a simple matter.


<div class="pagebar">
    <ul>
    
    <li id="pageinfo"><a>@Html.Raw(pageindex + 1) / @pagecount</a></li>
        @if (pageindex > 0)
        {
            <li><a href="@GetUrl(pageindex-1)">Previous</a> </li>
        }
        @{ spitindex = pageindex - 2;}
        @if (spitindex > 4)
        {
            
            <li><a href="@GetUrl(0)">1</a> </li>
            <li><a href="@GetUrl(spitindex - 2)">...</a> </li>
        }
        else
        {
            for (int i = 0; i < spitindex; i++)
            {
            
            <li><a href="@GetUrl(i)">@Html.Raw(i + 1)</a> </li>

            }
        }
        @for (int i = pageindex - 2; i < pageindex; i++)
        {
            if (i >= pageindex  i < 0)
            {
                continue;
            }
            
            <li><a href="@GetUrl(i)">@Html.Raw(i+1)</a> </li>
            
        }
        <li><a id="selectpage"><b>@Html.Raw(pageindex+1)</b> </a></li>
        @for (int i = pageindex + 1; i < pagecount; i++)
        {
            if (i >= pageindex + 3)
            {
                break;
            }
                 
            <li><a href="@GetUrl(i)">@Html.Raw(i+1)</a> </li>
            
        }
        @{ spitindex = pageindex + 3; }
        @if (pagecount - 4 > spitindex)
        {
            <li><a href="@GetUrl(spitindex + 2)">...</a> </li>
            <li><a href="@GetUrl(pagecount - 1)">@pagecount</a> </li>
        }
        else
        {
            for (int i = spitindex; i < pagecount; i++)
            {
                    
            <li><a href="@GetUrl(i)">@Html.Raw(i+1)</a> </li>
            }
        }
        @if (pageindex != pagecount - 1)
        {
                      
            <li><a href="@GetUrl(pageindex + 1)">Next</a> </li>
            
        }
    </ul>
</div>
Working with Controls


Control is very simple to use only in razor through renderpage output


@RenderPage ("~/views/controls/pagebar.cshtml", new {pageindex = Model.pageindex, PageCount = Model.pagecount})


Control effects







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.