Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sonnemaf/reflectionit.mvc.paging

ASP.NET Core 3.0 Paging (including filtering and sorting) solution using Entity Framework Core 3.0 and IEnumerable<T>
https://github.com/sonnemaf/reflectionit.mvc.paging

asp-net-core bootstrap4 csharp entity-framework-core paging

Last synced: 5 days ago
JSON representation

ASP.NET Core 3.0 Paging (including filtering and sorting) solution using Entity Framework Core 3.0 and IEnumerable<T>

Awesome Lists containing this project

README

        

# ReflectionIT.Mvc.Paging
ASP.NET Core 5.0, 6.0, 7.0 Paging (including filtering and sorting) solution using Entity Framework Core and IEnumerable

More info: https://reflectionit.nl/blog/2017/paging-in-asp-net-core-mvc-and-entityframework-core

# NuGet packages

| Package | Version |
| ------ | ------ |
| ReflectionIT.Mvc.Paging | [![NuGet](https://img.shields.io/nuget/v/ReflectionIT.Mvc.Paging)](https://www.nuget.org/packages/ReflectionIT.Mvc.Paging/) |

Use https://www.nuget.org/packages/ReflectionIT.Mvc.Paging/3.5.0 if you are still using ASP.NET Core 2.2

# Setup
Add the following code to the ConfigureServices() method of the Startup class. You can/should set the PageParameterName to 'pageindex' to solve the Page "Area" problem in ASP.NET Core 2.2 and higher. See https://github.com/sonnemaf/ReflectionIT.Mvc.Paging/issues/21

```
services.AddPaging(options => {
options.ViewName = "Bootstrap5";
options.PageParameterName = "pageindex";
});
```

# Controller
This Index action in this DemoController creates a PagingList and passes it as a Model to the View.

```
using Microsoft.AspNetCore.Mvc;
using ReflectionIT.Mvc.Paging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SampleApp.Controllers
{
public class DemoController : Controller {

private static readonly List _sampleData = GetSampleData();

private static List GetSampleData() {
return Enumerable.Range(1, 100).Select(n =>
new Models.DemoViewModel() {
Name = "Item" + n,
Number = n / 5
}).ToList();
}

public IActionResult Index(int pageindex = 1, string sort = "Name") {

var qry = from sd in _sampleData
where sd.Number > -5
select sd;

var model = PagingList.Create(qry, 10, pageindex, sort, "Name");

return View(model);
}
}
}
```

# View
The view has the PagingList as the Model. The **** renders the pagers using a ViewComponent. These are stored as Razor pager files inside the ReflectionIT.Mvc.Paging library. You can create your own Razor pager files in the Views\Shared\Components\Pager folder.

```xml
@using ReflectionIT.Mvc.Paging
@addTagHelper *, ReflectionIT.Mvc.Paging
@model PagingList

@{
ViewData["Title"] = "Demo";
}

Demo


Total Record Count: @Model.TotalRecordCount



@Html.SortableHeaderFor(model => model.Name)


@Html.SortableHeaderFor(model => model.Number, "Number,Name")

@foreach (var item in Model) {


@Html.DisplayFor(modelItem => item.Name)


@Html.DisplayFor(modelItem => item.Number)


}

```

# Model
```
public class DemoViewModel {

public string Name { get; set; }
public int Number { get; set; }

}
```