Skip to content

Commit 70ea8cf

Browse files
committed
Added example for area-enabled web apps
1 parent 0fc966d commit 70ea8cf

File tree

7 files changed

+162
-12
lines changed

7 files changed

+162
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@model IPagedList<string>
2+
3+
@using X.PagedList;
4+
@using X.Web.PagedList
5+
6+
@{
7+
ViewBag.Title = "Internal Admin Product Listing";
8+
Layout = "_Layout";
9+
}
10+
11+
<h3>Pager with Area links</h3>
12+
<ul>
13+
@foreach (var item in Model)
14+
{
15+
<li>
16+
@item
17+
</li>
18+
}
19+
</ul>
20+
21+
@Html.PagedListPager(
22+
(IPagedList)Model, page => Url.Action(ViewData["Action"].ToString(), new { page, area = "Internal" }))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>@ViewData["Title"] - Example.Website</title>
7+
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css" />
8+
<environment names="Development">
9+
<link rel="stylesheet" href="~/css/site.css" />
10+
<link rel="stylesheet" href="~/css/PagedList.css" />
11+
</environment>
12+
<environment names="Staging,Production">
13+
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
14+
</environment>
15+
</head>
16+
<body>
17+
<nav class="navbar navbar-inverse navbar-fixed-top">
18+
<div class="container">
19+
<div class="navbar-header">
20+
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
21+
<span class="sr-only">Toggle navigation</span>
22+
<span class="icon-bar"></span>
23+
<span class="icon-bar"></span>
24+
<span class="icon-bar"></span>
25+
</button>
26+
<a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">Example.Website</a>
27+
</div>
28+
<div class="navbar-collapse collapse">
29+
<ul class="nav navbar-nav">
30+
<li><a href="/">Back to Home</a></li>
31+
</ul>
32+
</div>
33+
</div>
34+
</nav>
35+
<div class="container body-content">
36+
@RenderBody()
37+
<hr />
38+
<footer>
39+
<p>&copy; @DateTime.Now.Year - Example.Website</p>
40+
</footer>
41+
</div>
42+
43+
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.2.0.min.js"></script>
44+
<script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/bootstrap.min.js"></script>
45+
<script src="/lib/jquery-ajax-unobtrusive/jquery.unobtrusive-ajax.js"></script>
46+
47+
48+
@RenderSection("Scripts", required: false)
49+
</body>
50+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Example.DAL;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Collections.Generic;
4+
using X.PagedList;
5+
6+
namespace Example.Website.Controllers;
7+
8+
[Area("Internal")]
9+
public class AdminController : Controller
10+
{
11+
private const int PageSize = 10;
12+
13+
public AdminController()
14+
{
15+
}
16+
17+
public IActionResult Index(int page = 1)
18+
{
19+
ViewBag.Names = GetPagedNames(page);
20+
21+
ViewData["Action"] = "Index";
22+
23+
var model = GetPagedNames(page);
24+
25+
return View(model);
26+
}
27+
28+
private IPagedList<string> GetPagedNames(int? page)
29+
{
30+
// return a 404 if user browses to before the first page
31+
if (page.HasValue && page < 1)
32+
{
33+
return null;
34+
}
35+
36+
// retrieve list from database/whereverand
37+
var listUnPaged = GetStuffFromFile();
38+
39+
// page the list
40+
41+
var listPaged = listUnPaged.ToPagedList(page ?? 1, PageSize);
42+
43+
// return a 404 if user browses to pages beyond last page. special case first page if no items exist
44+
if (listPaged.PageNumber != 1 && page.HasValue && page > listPaged.PageCount)
45+
{
46+
return null;
47+
}
48+
49+
return listPaged;
50+
}
51+
52+
/// <summary>
53+
/// In this case we return array of string, but in most DB situations you'll want to return IQueryable
54+
/// </summary>
55+
/// <returns></returns>
56+
private IEnumerable<string> GetStuffFromFile()
57+
{
58+
var sampleData = System.IO.File.ReadAllText("Names.txt");
59+
60+
return sampleData.Split('\n');
61+
}
62+
}

examples/Example.Website/Example.Website.csproj

+11
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,15 @@
1212
<ProjectReference Include="..\Example.DAL\Example.DAL.csproj"/>
1313
</ItemGroup>
1414

15+
<ItemGroup>
16+
<AdditionalFiles Include="Areas\Internal\Views\Shared\Error.cshtml" />
17+
<AdditionalFiles Include="Areas\Internal\Views\Shared\_Layout-41.cshtml" />
18+
<AdditionalFiles Include="Areas\Internal\Views\Shared\_Layout.cshtml" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<_ContentIncludedByDefault Remove="Areas\Internal\Views\Shared\Paging\_Pager.cshtml" />
23+
<_ContentIncludedByDefault Remove="Areas\Internal\Views\Shared\Paging\_Pager_85.cshtml" />
24+
</ItemGroup>
25+
1526
</Project>

examples/Example.Website/Program.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public static void Main(string[] args)
1717
builder.Services.AddDbContext<DatabaseContext>(options =>
1818
{
1919
var connectionString = $"Data Source={Path.Combine(builder.Environment.ContentRootPath, "data", "example.sqlite")}";
20-
20+
2121
options.UseSqlite(connectionString);
2222
});
2323

2424
var app = builder.Build();
25-
25+
2626
if (builder.Environment.IsDevelopment())
2727
{
2828
app.UseDeveloperExceptionPage();
@@ -33,16 +33,20 @@ public static void Main(string[] args)
3333
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
3434
app.UseHsts();
3535
}
36-
36+
3737
app.UseStaticFiles();
3838

3939
app.UseRouting();
4040

4141
app.UseAuthorization();
4242

4343
app.MapControllerRoute(
44-
name: "default",
45-
pattern: "{controller=Home}/{action=Index}/{id?}");
44+
name: "MyArea",
45+
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
46+
47+
app.MapControllerRoute(
48+
name: "default",
49+
pattern: "{controller=Home}/{action=Index}/{id?}");
4650

4751
app.Run();
4852
}

examples/Example.Website/Views/Home/Index.cshtml

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
<!-- import the included stylesheet for some (very basic) default styling -->
13-
<link href="/css/PagedList.css" rel="stylesheet" type="text/css" />
13+
<link href="/css/PagedList.css" rel="stylesheet" type="text/css"/>
1414

1515
<!-- loop through each of your products and display it however you want. we're just printing the name here -->
1616
<h2>List of Products</h2>
@@ -33,7 +33,7 @@
3333
@(Html.Pager(pagedList)
3434
.Url(page => Url.Action("Index", new { page }))
3535
.WithPartialView("Paging/_Pager_85")
36-
.DisplayLinkToFirstPage (PagedListDisplayMode.IfNeeded)
36+
.DisplayLinkToFirstPage(PagedListDisplayMode.IfNeeded)
3737
.DisplayLinkToLastPage(PagedListDisplayMode.IfNeeded)
3838
.DisplayLinkToPreviousPage()
3939
.DisplayLinkToNextPage()
@@ -44,11 +44,11 @@
4444
@Html.PagedListPager(pagedList,
4545
page => Url.Action("Index",
4646
new { page }),
47-
new PagedListRenderOptions()
48-
{
49-
DisplayItemSliceAndTotal = true,
50-
ItemSliceAndTotalPosition = ItemSliceAndTotalPosition.End
51-
})
47+
new PagedListRenderOptions()
48+
{
49+
DisplayItemSliceAndTotal = true,
50+
ItemSliceAndTotalPosition = ItemSliceAndTotalPosition.End
51+
})
5252

5353
<h3>Pager with ItemSliceAndTotalPosition at the beginning</h3>
5454
@Html.PagedListPager(pagedList,

examples/Example.Website/Views/Shared/_Layout.cshtml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<li><a asp-area="" asp-controller="Home" asp-action="Index">Traditional Paging</a></li>
3131
<li><a asp-area="" asp-controller="Bootstrap41" asp-action="Index">Traditional Paging (Bootstrap 4.1+)</a></li>
3232
<li><a asp-area="" asp-controller="EF" asp-action="Index">EF Core example</a></li>
33+
<li><a asp-area="Internal" asp-controller="Admin" asp-action="Index">Paging for Area enabled page</a></li>
3334
</ul>
3435
</div>
3536
</div>

0 commit comments

Comments
 (0)