Skip to content

Instantly share code, notes, and snippets.

@Teddybiers
Last active December 28, 2018 07:23
Show Gist options
  • Save Teddybiers/1821723df6d6bd681e8ea2bcd1ae015a to your computer and use it in GitHub Desktop.
Save Teddybiers/1821723df6d6bd681e8ea2bcd1ae015a to your computer and use it in GitHub Desktop.
.NET Core Linq extension for paging.
<!-- You can place this file in Shared/Components/Pager/Default.cshtml -->
@model PagedResultBase
@{
var urlTemplate = Url.Action() + "?page={0}";
var request = ViewContext.HttpContext.Request;
foreach (var key in request.Query.Keys)
{
if (key == "page")
{
continue;
}
urlTemplate += "&" + key + "=" + request.Query[key];
}
var startIndex = Math.Max(Model.CurrentPage - 5, 1);
var finishIndex = Math.Min(Model.CurrentPage + 5, Model.PageCount);
}
<!-- <div class="row">
<div class="col-md-8 col-sm-8 items-info">
Items @Model.FirstRowOnPage to @Model.LastRowOnPage of @Model.RowCount total
</div>
</div> -->
<div class="row d-flex justify-content-center margin-top-20">
<div>
@if (Model.PageCount > 1)
{
<ul class="pagination pull-right">
<li class="page-item"><a href="@urlTemplate.Replace("{0}", "1")" class="page-link">&laquo;</a></li>
@for (var i = startIndex; i <= finishIndex; i++)
{
@if (i == Model.CurrentPage)
{
<li class="page-item active"><span class="page-link">@i</span></li>
}
else
{
<li class="page-item"><a href="@urlTemplate.Replace("{0}", i.ToString())" class="page-link">@i</a></li>
}
}
<li class="page-item"><a href="@urlTemplate.Replace("{0}", Model.PageCount.ToString())" class="page-link">&raquo;</a></li>
</ul>
}
</div>
</div>
public IActionResult Search(DateTime? fromDate, DateTime? toDate, string keyword, int page = 1)
{
var bookings = _db.Bookings....'your conditions'.....GetPaged(page, 100);
return View("Index", bookings);
}
using System;
using System.Linq;
public static class LinqExtensions
{
public static PagedResult<T> GetPaged<T>(this IQueryable<T> query,
int page, int pageSize = 25) where T : class
{
var result = new PagedResult<T>();
result.CurrentPage = page;
result.PageSize = pageSize;
result.RowCount = query.Count();
var pageCount = (double)result.RowCount / pageSize;
result.PageCount = (int)Math.Ceiling(pageCount);
var skip = (page - 1) * pageSize;
result.Results = query.Skip(skip).Take(pageSize).ToList();
return result;
}
}
using System;
using System.Collections.Generic;
public abstract class PagedResultBase
{
public int CurrentPage { get; set; }
public int PageCount { get; set; }
public int PageSize { get; set; }
public int RowCount { get; set; }
public int FirstRowOnPage
{
get { return (CurrentPage - 1) * PageSize + 1; }
}
public int LastRowOnPage
{
get { return Math.Min(CurrentPage * PageSize, RowCount); }
}
}
public class PagedResult<T> : PagedResultBase where T : class
{
public IList<T> Results { get; set; }
public PagedResult()
{
Results = new List<T>();
}
}
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
public class PagerViewComponent : ViewComponent
{
public Task<IViewComponentResult> InvokeAsync(PagedResultBase result)
{
return Task.FromResult((IViewComponentResult)View("Default", result));
}
}
@model PagedResult<Booking
... Your HTML..
<tbody>
@foreach (var item in Model.Results)
{
<tr>
<th>@item.Id</th>
</tr>
}
</tbody>
...
@(await Component.InvokeAsync<PagerViewComponent>(Model))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment