Created
May 29, 2012 17:20
-
-
Save jarrettmeyer/2829612 to your computer and use it in GitHub Desktop.
Simple Pagination with MVC3 + Twitter Bootstrap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @model IPaginate | |
| <div class="pagination pagination-centered"> | |
| <ul> | |
| @for (int i = 1; i <= Model.PageCount; i += 1) | |
| { | |
| string cls = ""; | |
| if (i == Model.Page) | |
| { | |
| cls += " active"; | |
| } | |
| <li class="@cls"><a href="@GetPagedUrl(i)">@i</a></li> | |
| } | |
| </ul> | |
| </div> | |
| @functions | |
| { | |
| private bool HasPageInQuery | |
| { | |
| get | |
| { | |
| var url = Request.Url; | |
| if (url == null) | |
| return false; | |
| var query = url.Query; | |
| return System.Text.RegularExpressions.Regex.IsMatch(query, @"[?&]Page=\d+"); | |
| } | |
| } | |
| private bool HasQuery | |
| { | |
| get | |
| { | |
| var url = Request.Url; | |
| if (url == null) | |
| return false; | |
| return !string.IsNullOrEmpty(url.Query); | |
| } | |
| } | |
| private string GetPagedUrl(int page) | |
| { | |
| var pageQuery = string.Format("Page={0}", page); | |
| if (HasPageInQuery) | |
| return System.Text.RegularExpressions.Regex.Replace(Request.Url.Query, @"Page=\d+", pageQuery); | |
| if (HasQuery) | |
| return Request.Url + "&" + pageQuery; | |
| return Request.Url + "?" + pageQuery; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// Indicates that a model provides support for pagination. | |
| /// See http://twitter.github.com/bootstrap/components.html#pagination. | |
| /// </summary> | |
| public interface IPaginate | |
| { | |
| /// <summary> | |
| /// Gets the current page in the record set. | |
| /// </summary> | |
| int Page { get; } | |
| /// <summary> | |
| /// Gets the total number of pages in the record set. | |
| /// </summary> | |
| int PageCount { get; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment