PagedList.MVC

Manual Paging

Controller

namespace WebApplication1.Controllers
{
   public class HomeController : Controller
   {
       public ActionResult Index(int? page)
       {
           var pageIndex =( page ?? 1) - 1;
           var pageSize = 2;

           var dsn = "Server=hv-cent01;Port=5432;User Id=taro;Password=mypass;Database=mydb";
           var cn = new NpgsqlConnection(dsn);
           cn.Open();

           var count = (int) cn.Query<Int64>(@"SELECT COUNT(*) AS count FROM t1").Single();

           var rows = cn.Query<MyEntity>(@"SELECT * FROM t1 OFFSET @offset LIMIT @limit",
               new { offset = pageIndex * pageSize, limit = pageSize });
           var pagedList = new StaticPagedList<MyEntity>(rows, pageIndex + 1, pageSize, (int) count);
           ViewBag.Rows = pagedList;

           return View();
       }
   }
}

View

@{
    ViewBag.Title = "Index";
}
@using PagedList.Mvc;
@using PagedList;

<link href="~/Content/PagedList.css" rel="stylesheet" />

<h1>@ViewBag.Title</h1>
<ul>
    @foreach (var row in ViewBag.Rows)
    {
        <li>@row.Id @row.Name</li>
    }
</ul>

@Html.PagedListPager((IPagedList)ViewBag.rows, page => Url.Action("Index", new { page }))

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2014-02-02 (日) 22:00:53 (1776d)