-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from dncuug/230
Update implementation for IQueryable
- Loading branch information
Showing
41 changed files
with
344 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Example.DAL; | ||
|
||
public partial class Animal | ||
{ | ||
public long Id { get; set; } | ||
|
||
public string Name { get; set; } = null!; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Example.DAL; | ||
|
||
public partial class DatabaseContext : DbContext | ||
{ | ||
public DatabaseContext() | ||
{ | ||
} | ||
|
||
public DatabaseContext(DbContextOptions<DatabaseContext> options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public virtual DbSet<Animal> Animals { get; set; } | ||
|
||
public virtual DbSet<User> Users { get; set; } | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
// Only configure the DbContext if it hasn't been configured yet | ||
if (!optionsBuilder.IsConfigured) | ||
{ | ||
// DbContext is not yet configured, configure it now | ||
optionsBuilder.UseSqlite("Data Source=<path_to_database_file>"); | ||
} | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Animal>(entity => | ||
{ | ||
entity.ToTable("Animal"); | ||
}); | ||
|
||
modelBuilder.Entity<User>(entity => | ||
{ | ||
entity.ToTable("User"); | ||
}); | ||
|
||
OnModelCreatingPartial(modelBuilder); | ||
} | ||
|
||
partial void OnModelCreatingPartial(ModelBuilder modelBuilder); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Example.DAL; | ||
|
||
public partial class User | ||
{ | ||
public long Id { get; set; } | ||
|
||
public string Name { get; set; } = null!; | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/Example.Website/Controllers/Bootstrap41Controller.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Example.DAL; | ||
|
||
namespace Example.Website.Controllers; | ||
|
||
public class Bootstrap41Controller : HomeController | ||
{ | ||
public Bootstrap41Controller(DatabaseContext databaseContext) : base(databaseContext) | ||
{ | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Example.DAL; | ||
using Microsoft.AspNetCore.Mvc; | ||
using X.PagedList; | ||
|
||
namespace Example.Website.Controllers; | ||
|
||
public class HomeController : Controller | ||
{ | ||
private const int PageSize = 10; | ||
|
||
private readonly DatabaseContext _databaseContext; | ||
|
||
public HomeController(DatabaseContext databaseContext) | ||
{ | ||
_databaseContext = databaseContext; | ||
} | ||
|
||
public IActionResult Index(int page = 1) | ||
{ | ||
ViewBag.Names = GetPagedNames(page); | ||
return View(); | ||
} | ||
|
||
public IActionResult AjaxIndex(int page = 1) | ||
{ | ||
var listPaged = GetPagedNames(page); | ||
ViewBag.Names = listPaged; | ||
return View(); | ||
} | ||
|
||
public async Task<IActionResult> EFCore(int page = 1) | ||
{ | ||
// return a 404 if user browses to before the first page | ||
if (page < 1) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
var records = await _databaseContext.Animals | ||
.Select(o => o.Name) | ||
.ToPagedListAsync(page, PageSize); | ||
|
||
// return a 404 if user browses to pages beyond last page. special case first page if no items exist | ||
if (records.PageNumber != 1 && page > records.PageCount) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
ViewBag.Names = records; | ||
|
||
return View(); | ||
} | ||
|
||
public IActionResult GetOnePageOfNames(int page = 1) | ||
{ | ||
var listPaged = GetPagedNames(page); | ||
ViewBag.Names = listPaged; | ||
return PartialView("_NameListPartial", ViewBag.Names); | ||
} | ||
|
||
public IActionResult Error() | ||
{ | ||
return View(); | ||
} | ||
|
||
private IPagedList<string> GetPagedNames(int? page) | ||
{ | ||
// return a 404 if user browses to before the first page | ||
if (page.HasValue && page < 1) | ||
{ | ||
return null; | ||
} | ||
|
||
// retrieve list from database/whereverand | ||
var listUnPaged = GetStuffFromFile(); | ||
|
||
// page the list | ||
|
||
var listPaged = listUnPaged.ToPagedList(page ?? 1, PageSize); | ||
|
||
// return a 404 if user browses to pages beyond last page. special case first page if no items exist | ||
if (listPaged.PageNumber != 1 && page.HasValue && page > listPaged.PageCount) | ||
{ | ||
return null; | ||
} | ||
|
||
return listPaged; | ||
} | ||
|
||
/// <summary> | ||
/// In this case we return array of string, but in most DB situations you'll want to return IQueryable | ||
/// </summary> | ||
/// <returns></returns> | ||
private IEnumerable<string> GetStuffFromFile() | ||
{ | ||
var sampleData = System.IO.File.ReadAllText("Names.txt"); | ||
return sampleData.Split('\n'); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
....Core/X.PagedList.Mvc.Example.Core.csproj → ...es/Example.Website/Example.Website.csproj
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks> | ||
<TargetFramework>net7.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\X.PagedList.Mvc.Core\X.PagedList.Mvc.Core.csproj" /> | ||
<ProjectReference Include="..\..\src\X.PagedList\X.PagedList.csproj" /> | ||
<ProjectReference Include="..\Example.DAL\Example.DAL.csproj" /> | ||
</ItemGroup> | ||
</Project> |
File renamed without changes.
This file contains 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 renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
@{ | ||
ViewBag.Title = "Product Listing"; | ||
var pagedList = (IPagedList)ViewBag.Names; | ||
} | ||
|
||
@using X.PagedList.Mvc.Core; @*import this so we get our HTML Helper*@ | ||
@using X.PagedList; @*import this so we can cast our list to IPagedList (only necessary because ViewBag is dynamic)*@ | ||
@using X.PagedList.Mvc.Core.Fluent | ||
@using X.PagedList.Web.Common | ||
|
||
|
||
<!-- import the included stylesheet for some (very basic) default styling --> | ||
<link href="/css/PagedList.css" rel="stylesheet" type="text/css" /> | ||
|
||
<!-- loop through each of your products and display it however you want. we're just printing the name here --> | ||
<h2>List of Products from EF Core</h2> | ||
<ul> | ||
@foreach (var name in ViewBag.Names) | ||
{ | ||
<li>@name</li> | ||
} | ||
</ul> | ||
|
||
<!-- output a paging control that lets the user navigation to the previous page, next page, etc --> | ||
@Html.PagedListPager(pagedList, page => Url.Action("EFCore", new { page })) | ||
|
||
<h3>Fluent pager</h3> | ||
@(Html.Pager(pagedList) | ||
.Url(page => Url.Action("EFCore", new { page })) | ||
.Build()) | ||
|
||
<h3>Pager for #85</h3> | ||
@(Html.Pager(pagedList) | ||
.Url(page => Url.Action("EFCore", new { page })) | ||
.WithPartialView("Paging/_Pager_85") | ||
.DisplayLinkToFirstPage (PagedListDisplayMode.IfNeeded) | ||
.DisplayLinkToLastPage(PagedListDisplayMode.IfNeeded) | ||
.DisplayLinkToPreviousPage() | ||
.DisplayLinkToNextPage() | ||
.MaximumPageNumbersToDisplay(3) | ||
.Build()) | ||
|
||
<h3>Pager with ItemSliceAndTotalPosition at the end</h3> | ||
@Html.PagedListPager(pagedList, | ||
page => Url.Action("EFCore", new { page }), | ||
new PagedListRenderOptions() | ||
{ | ||
DisplayItemSliceAndTotal = true, | ||
ItemSliceAndTotalPosition = ItemSliceAndTotalPosition.End | ||
}) | ||
|
||
<h3>Pager with ItemSliceAndTotalPosition at the beginning</h3> | ||
@Html.PagedListPager(pagedList, | ||
page => Url.Action("EFCore", new { page }), | ||
new PagedListRenderOptions() | ||
{ | ||
DisplayItemSliceAndTotal = true, | ||
}) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
Oops, something went wrong.