Skip to content

Instantly share code, notes, and snippets.

@mombrea
Created August 4, 2015 18:46
Show Gist options
  • Save mombrea/2892d5442c0688ccf403 to your computer and use it in GitHub Desktop.
Save mombrea/2892d5442c0688ccf403 to your computer and use it in GitHub Desktop.
Razor view for paging and sorting a table
@model PagedList.IPagedList<My.App.Model>
@using PagedList.Mvc;
@{
ViewBag.Title = "Title";
string currentFilter = ViewBag.CurrentFilter;
string currentSort = ViewBag.CurrentSort;
if (string.IsNullOrEmpty(currentSort))
{
currentSort = "date_desc";
}
}
<div class="wrapper">
<div class="search-box">
<form class="form-inline">
<div class="form-group">
<input type="text" name="q" value="@ViewBag.searchQuery" class="search-text form-control" placeholder="Search..." />
</div>
<button type="submit" class="btn btn-info">Search</button>
</form>
</div>
<div class="table-responsive">
<table class="table table-striped table-condensed table-hover">
<tr>
<th>
@Html.ActionLink("name", "Index", Request.QueryString.ToRouteValueDictionary("sortOrder", (string)ViewBag.NameSortParam))
@Html.SortIdentifier(currentSort, "name")
</th>
<th>
@Html.ActionLink("Address", "Index", Request.QueryString.ToRouteValueDictionary("sortOrder", (string)ViewBag.AddressSortParam))
@Html.SortIdentifier(currentSort, "address")
</th>
<th>
@Html.ActionLink("Date", "Index", Request.QueryString.ToRouteValueDictionary("sortOrder", (string)ViewBag.DateSortParam))
@Html.SortIdentifier(currentSort, "date")
</th>
</tr>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.address)
</td>
<td>
@Html.DisplayFor(modelItem => item.date)
</td>
</tr>
}
</tbody>
</table>
</div>
@if (Model.PageCount > 1)
{
<div class="pager">
@Html.PagedListPager(Model, page => Url.Action("Index", new
{
page,
sortOrder = ViewBag.currentSort,
q = currentSearch
}))
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
</div>
}
</div>
@weedkiller
Copy link

weedkiller commented Jan 21, 2018

Hi, nice helper what does the controller or helper look like, is it taking an argument.

  • Also is there an option for Ajax
  • Can you include ValidateAntiForgery By default built in as part of this
  • How can this be used inside a partial view
  • update to x.pagelist guess its changed now from ipaged list

thnaks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment