Created
October 27, 2017 09:36
-
-
Save ps-team/cc312ed1de13ed08916754dca577d308 to your computer and use it in GitHub Desktop.
An example of how the app_code folder can be used to create and home re-usable html helpers that razorviews can use. These would be .cshtml files.
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
@* | |
* FILE : /App_Code/HtmlHelpers.cshtml | |
*@ | |
@using System.Collections.Specialized | |
@helper RenderPagination(int pageNumber, int pageCount) | |
{ | |
@* | |
* HELPER : RENDER PAGINATION | |
* ************************** | |
* Description : takes an input 'current page number' and 'page count' to | |
* work at what point in the pagination we are at and the | |
* pagination size | |
* | |
* Example use : a site that has multiple custom listing razorviews (search, | |
* news, events etc) that all shared the same html structure | |
* for pagination and query-string parameters | |
*@ | |
<ul> | |
@{ | |
// previous link | |
if(pageNumber > 1) | |
{ | |
// url | |
NameValueCollection qs = HttpUtility.ParseQueryString(Request.QueryString.ToString()); | |
qs.Set("page", (pageNumber - 1).ToString()); | |
string url = Request.Url.AbsolutePath + "?" + qs.ToString(); | |
<li class="previous"> | |
<a href="@url" title="Previous page">‹</a> | |
</li> | |
} | |
// page links | |
for(int i = 1; i <= pageCount; i++) | |
{ | |
int rangeMin = pageNumber - 4; | |
int rangeMax = pageNumber + 4; | |
if(i >= rangeMin && i <= rangeMax) | |
{ | |
// css | |
string css = (i == pageNumber) ? "selected" : null; | |
// url | |
NameValueCollection qs = HttpUtility.ParseQueryString(Request.QueryString.ToString()); | |
qs.Set("page", i.ToString()); | |
string url = Request.Url.AbsolutePath + "?" + qs.ToString(); | |
<li class="@css"> | |
<a href="@url">@i</a> | |
</li> | |
} | |
} | |
// next link | |
if (pageNumber < pageCount) | |
{ | |
// url | |
NameValueCollection qs = HttpUtility.ParseQueryString(Request.QueryString.ToString()); | |
qs.Set("page", (pageNumber + 1).ToString()); | |
string url = Request.Url.AbsolutePath + "?" + qs.ToString(); | |
<li class="next"> | |
<a href="@url" title="Next page">›</a> | |
</li> | |
} | |
} | |
</ul> | |
} | |
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
@* | |
* FILE : /SiteElements/Razorviews/MyListingRazorview.cshtml | |
*@ | |
@{ | |
@* | |
* NOTES | |
* ***** | |
* To call html helpers we refer to the file name and helper name: | |
* | |
* Example : FileName.HelperName(); | |
*@ | |
// query-string parameters | |
int QS_PAGE = Request.QueryString["page"].AsInt() > 0 ? Request.QueryString["page"].AsInt() : 1; | |
// calculated parameters | |
int NUMBER_OF_PAGES = 20; // this would normally be calculated by using the result count and a formula (below), but is hard-coded for this example | |
// formula | |
NUMBER_OF_PAGES = NUMBER_OF_RESULTS / DISPLAY; | |
if(NUMBER_OF_RESULTS % DISPLAY != 0) | |
{ | |
NUMBER_OF_PAGES = NUMBER_OF_PAGES + 1; | |
} | |
START_INDEX = (QS_PAGE * DISPLAY) - DISPLAY; | |
@HtmlHelpers.RenderPagination(QS_PAGE, NUMBER_OF_PAGES) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment