Skip to content

Commit 084a51b

Browse files
committed
fixed selected page style, added pager tag helper
1 parent 5795cd9 commit 084a51b

File tree

8 files changed

+139
-13
lines changed

8 files changed

+139
-13
lines changed

DotNetPaging/DotNetPaging.AspNetCore/Controllers/HomeController.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,12 @@ public async Task<IActionResult> AutoMapper(int page = 1)
4141
return View(releases);
4242
}
4343

44-
public IActionResult About()
44+
public async Task<IActionResult> TagHelper(int page = 1)
4545
{
46-
ViewData["Message"] = "Your application description page.";
47-
48-
return View();
49-
}
50-
51-
public IActionResult Contact()
52-
{
53-
ViewData["Message"] = "Your contact page.";
54-
55-
return View();
46+
var releases = await _dataContext.PressReleases
47+
.OrderByDescending(p => p.ReleaseDate)
48+
.GetPagedAsync(page, 10);
49+
return View(releases);
5650
}
5751

5852
public IActionResult Error()

DotNetPaging/DotNetPaging.AspNetCore/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using DotNetPaging.EFCore;
33
using Microsoft.AspNetCore.Builder;
44
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.AspNetCore.Mvc.Infrastructure;
56
using Microsoft.EntityFrameworkCore;
67
using Microsoft.Extensions.Configuration;
78
using Microsoft.Extensions.DependencyInjection;
@@ -25,6 +26,8 @@ public void ConfigureServices(IServiceCollection services)
2526

2627
services.AddAutoMapper();
2728
services.AddMvc();
29+
30+
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
2831
}
2932

3033
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using System.Net;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.AspNetCore.Mvc.Infrastructure;
6+
using Microsoft.AspNetCore.Mvc.Rendering;
7+
using Microsoft.AspNetCore.Mvc.Routing;
8+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
9+
using Microsoft.AspNetCore.Razor.TagHelpers;
10+
11+
namespace DotNetPaging.AspNetCore.TagHelpers
12+
{
13+
[HtmlTargetElement("pager", TagStructure = TagStructure.NormalOrSelfClosing)]
14+
public class PagerTagHelper : TagHelper
15+
{
16+
private readonly HttpContext _httpContext;
17+
private readonly IUrlHelper _urlHelper;
18+
19+
[ViewContext]
20+
public ViewContext ViewContext { get; set; }
21+
22+
public PagerTagHelper(IHttpContextAccessor accessor, IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory)
23+
{
24+
_httpContext = accessor.HttpContext;
25+
_urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
26+
}
27+
28+
[HtmlAttributeName("pager-model")]
29+
public PagedResultBase Model { get; set; }
30+
31+
public override void Process(TagHelperContext context, TagHelperOutput output)
32+
{
33+
if(Model == null)
34+
{
35+
return;
36+
}
37+
38+
if(Model.PageCount == 0)
39+
{
40+
return;
41+
}
42+
43+
var action = ViewContext.RouteData.Values["action"].ToString();
44+
var urlTemplate = WebUtility.UrlDecode(_urlHelper.Action(action, new { page = "{0}" }));
45+
var request = _httpContext.Request;
46+
foreach (var key in request.Query.Keys)
47+
{
48+
if (key == "page")
49+
{
50+
continue;
51+
}
52+
53+
urlTemplate += "&" + key + "=" + request.Query[key];
54+
}
55+
56+
var startIndex = Math.Max(Model.CurrentPage - 5, 1);
57+
var finishIndex = Math.Min(Model.CurrentPage + 5, Model.PageCount);
58+
59+
output.Content.AppendHtml("<ul class=\"pagination\">");
60+
AddPageLink(output, string.Format(urlTemplate, 1), "&laquo;");
61+
62+
for (var i = startIndex; i <= finishIndex; i++)
63+
{
64+
if (i == Model.CurrentPage)
65+
{
66+
AddCurrentPageLink(output, i);
67+
}
68+
else
69+
{
70+
AddPageLink(output, string.Format(urlTemplate, i), i.ToString());
71+
}
72+
}
73+
74+
AddPageLink(output, string.Format(urlTemplate, Model.PageCount), "&raquo;");
75+
output.Content.AppendHtml("</ul>");
76+
}
77+
78+
private void AddPageLink(TagHelperOutput output, string url, string text)
79+
{
80+
output.Content.AppendHtml("<li><a href=\"");
81+
output.Content.AppendHtml(url);
82+
output.Content.AppendHtml("\">");
83+
output.Content.AppendHtml(text);
84+
output.Content.AppendHtml("</a>");
85+
output.Content.AppendHtml("</li>");
86+
}
87+
88+
private void AddCurrentPageLink(TagHelperOutput output, int page)
89+
{
90+
output.Content.AppendHtml("<li class=\"active\">");
91+
output.Content.AppendHtml("<span>");
92+
output.Content.AppendHtml(page.ToString());
93+
output.Content.AppendHtml("</span>");
94+
output.Content.AppendHtml("</li>");
95+
}
96+
}
97+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@model PagedResult<PressRelease>
2+
@{
3+
ViewData["Title"] = "TagHelper";
4+
}
5+
6+
<h2>Default paging</h2>
7+
8+
<p>Results on this page are queried using synchronous method calls of Entity Framework Core.</p>
9+
10+
<table class="table">
11+
<thead>
12+
<tr>
13+
<th>Date</th>
14+
<th>Company</th>
15+
<th>Title</th>
16+
</tr>
17+
</thead>
18+
<tbody>
19+
@foreach (var release in Model.Results)
20+
{
21+
<tr>
22+
<td>@release.ReleaseDate.ToShortDateString()</td>
23+
<td>@release.Company</td>
24+
<td>@release.Title</td>
25+
</tr>
26+
}
27+
</tbody>
28+
</table>
29+
30+
<pager pager-model="@Model"></pager>

DotNetPaging/DotNetPaging.AspNetCore/Views/Shared/Components/Pager/Default.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
@if (i == Model.CurrentPage)
2828
{
2929

30-
<li><span>@i</span></li>
30+
<li class="active"><span>@i</span></li>
3131
}
3232
else
3333
{

DotNetPaging/DotNetPaging.AspNetCore/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
3434
<li><a asp-area="" asp-controller="Home" asp-action="Async">Async</a></li>
3535
<li><a asp-area="" asp-controller="Home" asp-action="AutoMapper">AutoMapper</a></li>
36+
<li><a asp-area="" asp-controller="Home" asp-action="TagHelper">TagHelper</a></li>
3637
</ul>
3738
</div>
3839
</div>

DotNetPaging/DotNetPaging.AspNetCore/Views/_ViewImports.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
@using DotNetPaging.AspNetCore.Models
55
@using DotNetPaging.EFCore
66
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
7+
@addTagHelper *, DotNetPaging.AspNetCore

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Sample solution contains the following examples:
1111
* Extension methods for pages data with AutoMapper mapping
1212
* ASP.NET Core 2.0 web application (Entity Framework Core)
1313
* ASP.NET MVC (Entity Framework)
14-
* Pager view component and partial view
14+
* Pager view component, tag helper and partial view
1515
* Database script for SQL Server
1616

1717
# Read more about paging

0 commit comments

Comments
 (0)