ASP.NET MVC 4 View ( Razor ) + Paging ( PagedList.MVC ) + AJAX Pager 方法

ASP.NET MVC 4 View ( Razor ) + Paging ( PagedList.MVC ) + AJAX Pager 方法
 
Add ( PagedList.MVC ) NuGet Package on MVC Web Application Project

   \Controllers\HomeController.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.Mvc;
   … …
   using MvcApplication.Models;
   using PagedList;
 
   namespace MvcApplication.Controllers
   {
      public class HomeController : Controller
      {
 
         … …
 
         public ActionResult ProductCategoryList(String id, int? p)
         {
            if (!p.HasValue) p = 1;
 
            if (id != null)
            {
               List<Models.Product> list = …………………………………… ;
               var data = list.ToPagedList(pageNumber:p.Value, pageSize:10);
               return View(data);
            }
            else
            {
               return RedirectToAction("Index");
            }
         }
 
      }
   }
 
   \Views\ProductCategoryList.cshtml
 
   @using PagedList
   @using PagedList.Mvc
   @model IEnumerable<MvcApplication.Models.Product>
   @{
      Layout = "~/Views/Shared/Layout.cshtml";
   }
 
   <link href="~/Content/PagedList.css" rel="stylesheet" />
 
   @if (Model != null)
   {
 
      var data = Model as IPagedList<MvcApplication.Models.Product>;
 
      <table id="ProductTab" border="0" cellpadding="0" cellspacing="0" width="100%" style="margin-left:20px;">
 
         <tr>
            <th valign="top">Subcat</th>
            <th valign="top">Model</th>
            <th valign="top">Price</th>
         </tr>
 
         @foreach (MvcApplication1.Models.Product item in data)
         {
            <tr>
               <td width="27%" valign="top">( @(item.subcat) )</td>
               <td valign="top">@(item.model)</td>
               <td width="15%" valign="top">$@(item.price)</td>
            </tr>
         }
 
      </table>
 
      <div style="margin-left:20px;">
         @Html.PagedListPager(list: data,
                                             generatePageUrl: page => Url.Action("ProductCategoryList", new { p = page }),
                                             options: PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing( new AjaxOptions() {
                                                HttpMethod = "GET", UpdateTargetId = "ProductPanel"
                                             }))
      </div>
 
   }