Coder Social home page Coder Social logo

paulczy / reflectionit.mvc.paging Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sonnemaf/reflectionit.mvc.paging

0.0 0.0 0.0 4.75 MB

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

License: MIT License

C# 9.39% JavaScript 6.50% CSS 35.44% Ruby 0.29% HTML 48.25% PowerShell 0.13%

reflectionit.mvc.paging's Introduction

ReflectionIT.Mvc.Paging

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

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

You can also use the NuGet package 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

Install

Install-Package ReflectionIT.Mvc.Paging

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 sonnemaf#21

services.AddPaging(options => {
    options.ViewName = "Bootstrap4";
    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<Models.DemoViewModel> _sampleData = GetSampleData();

        private static List<Models.DemoViewModel> 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 <vc:pager paging-list="@Model"></vc:pager> 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.

@using ReflectionIT.Mvc.Paging
@addTagHelper *, ReflectionIT.Mvc.Paging
@model PagingList<SampleApp.Models.DemoViewModel>

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

<h1>Demo</h1>
Total Record Count: @Model.TotalRecordCount
<nav aria-label="Products navigation example">
    <vc:pager paging-list="@Model"></vc:pager>
</nav>

<table class="table">
    <tr>
        <th>
            @Html.SortableHeaderFor(model => model.Name)
        </th>
        <th>
            @Html.SortableHeaderFor(model => model.Number, "Number,Name")
        </th>
    </tr>

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Number)
            </td>
        </tr>
    }
</table>

<nav aria-label="Products navigation example">
    <vc:pager paging-list="@Model" />
</nav>

Model

public class DemoViewModel {

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

}

reflectionit.mvc.paging's People

Contributors

sonnemaf avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.