Fork me on GitHub

MvcPager demo - Using custom routes

This sample demonstrates how MvcPager can generate different paging urls using different routes.

This demo uses three MvcPagers with different routes, each MvcPager generate different paging url except for the first page because the first page url SEO is enabled.

Order ID Customer ID Company Name Employee Name
10760 MAISD Maison Dewey Margaret Peacock
10761 RATTC Rattlesnake Canyon Grocery Steven Buchanan
10762 FOLKO Folk och fä HB Janet Leverling
10763 FOLIG Folies gourmandes Janet Leverling
10764 ERNSH Ernst Handel Michael Suyama
Using route MvcPager_Pager1, url pattern: {controller}/{action}/page_{pageindex}
First Prev 1 2 3 4 5 6 7 8 9 10 ... Next Last 

Using route MvcPager_Pager2, url pattern: {controller}/{action}/pageindex-{pageindex}
First Prev 1 2 3 4 5 6 7 8 9 10 ... Next Last 

Using route MvcPager_Pager3, url pattern: {controller}/{action}/p-{pageindex}
First Prev 1 2 3 4 5 6 7 8 9 10 ... Next Last 

View:

        
@model PagedList<Order>
@Html.Partial("_DataTable", Model)
<h6>Using route MvcPager_Pager1, url pattern: {controller}/{action}/page_{pageindex}</h6>
    @Html.Pager(Model).Options(o => o.SetFirstPageRouteName("MvcPager_Default").SetRouteName("MvcPager_Pager1").SetPagerItemTemplate("{0}&nbsp;"))
<hr/>
<h6>Using route MvcPager_Pager2, url pattern: {controller}/{action}/pageindex-{pageindex}</h6>
@Html.Pager(Model).Options(o => o.SetFirstPageRouteName("MvcPager_Default").SetRouteName("MvcPager_Pager2").SetPagerItemTemplate("{0}&nbsp;"))
<hr/>
<h6>Using route MvcPager_Pager3, url pattern: {controller}/{action}/p-{pageindex}</h6>
@Html.Pager(Model).Options(o => o.SetRouteName("MvcPager_Pager3").SetFirstPageRouteName("MvcPager_Default").SetPagerItemTemplate("{0}&nbsp;"))

_DataTable.cshtml:

@model PagedList<order>
<table class="table table-striped table-bordered">
    <tr>
    <th class="nowrap">
            @Html.DisplayNameFor(model => model.OrderId)
        </th>
    <th class="nowrap">
            @Html.DisplayNameFor(model => model.CustomerId)
        </th>
    <th class="nowrap">
            @Html.DisplayNameFor(model => model.CompanyName)
        </th>
    <th class="nowrap">
            @Html.DisplayNameFor(model => model.EmployeeName)
        </th>
    </tr>
    @foreach (var item in Model)
    {
    <tr>
    <td>
                @Html.DisplayFor(modelItem => item.OrderId)
            </td>
    <td>
                @Html.DisplayFor(modelItem => item.CustomerId)
            </td>
    <td>
                @Html.DisplayFor(modelItem => item.CompanyName)
            </td>
    <td>
                @Html.DisplayFor(modelItem => item.EmployeeName)
            </td>
        </tr>
    }
</table>

Model:

    public class Order
    {
        [Display(Name = "Order ID")]
        public int OrderId { get; set; }
        [Display(Name = "Customer ID"),StringLength(20)]
        public string CustomerId { get; set; }
        [Display(Name = "Order Date")]
        public DateTime OrderDate { get; set; }
        [Display(Name = "Company Name"), StringLength(20)]
        public string CompanyName { get; set; }
        [Display(Name = "Employee Name"), StringLength(20)]
        public string EmployeeName { get; set; }
    }

Controller:

        
        public ActionResult CustomRouting(int pageindex = 1)
        {
            using (var db = new DataContext())
            {
                return View(db.Orders.OrderBy(o=>o.OrderId).ToPagedList(pageindex, 5));
            }
        }