Skip to content

Instantly share code, notes, and snippets.

@relyky
Last active March 1, 2021 09:50
Show Gist options
  • Save relyky/b8dfa47b864121c3aaf5f74b09b84fa1 to your computer and use it in GitHub Desktop.
Save relyky/b8dfa47b864121c3aaf5f74b09b84fa1 to your computer and use it in GitHub Desktop.
MVC5 Pagination, PagedList.Mvc, 分頁
using PagedList;
namespace YourProject.Controllers
{
public class ComputerController : Controller
{
private MineDBEntities db = new MineDBEntities();
// GET: Computer
public ActionResult Index(int? page)
{
// ~~return View(db.電腦設備登記表.ToList());~~
var qry = db.電腦設備登記表.OrderByDescending(c => c.登記日期);
int pageSize = 5;
int pageNumber = (page ?? 1);
return View(qry.ToPagedList(pageNumber, pageSize));
}
[HttpPost]
public PartialViewResult LoadIndexContent(int page)
{
var qry = db.電腦設備登記表.OrderByDescending(c => c.登記日期);
int pageSize = 5;
int pageNumber = page;
return PartialView("_IndexContent", qry.ToPagedList(pageNumber, pageSize));
}
}
}
/// filename: _IndexContent.cshtml
@* ~~@model IEnumerable<MineAsset.Biz.電腦設備登記表>~~ *@
@model PagedList.IPagedList<MineAsset.Biz.電腦設備登記表>
@using PagedList.Mvc;
<table class="table">
... to render data model ...
</table>
<div class="text-center">
@Html.PagedListPager(Model, page => Url.Action("LoadIndexContent", new { page }),
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing( //<--- 啟動 ajax 通訊機制
new PagedListRenderOptions
{
DisplayPageCountAndCurrentLocation = true,
PageCountAndCurrentLocationFormat = "第{0}/{1}頁"
},
new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
UpdateTargetId = "id_IndexContent"
}
))
</div>
/// filename: Index.cshtml
@* ~~@model IEnumerable<MineAsset.Biz.電腦設備登記表>~~ *@
@model PagedList.IPagedList<MineAsset.Biz.電腦設備登記表>
@using PagedList.Mvc;
@* AjaxHelper 需與 Partial View 搭配 *@
<div id="id_IndexContent">
@Html.Partial("_IndexContent", Model)
</div>
using PagedList;
namespace YourProject.Controllers
{
public class StuffController : Controller
{
// GET: 消耗品登記表
public ActionResult Index(int? page)
{
// ~~return View(db.消耗品登記表.ToList());~~
var qry = db.消耗品登記表.OrderByDescending(c => c.登記日期);
int pageSize = 5;
int pageNumber = (page ?? 1);
return View(qry.ToPagedList(pageNumber, pageSize));
}
}
}
/// filename: Index.cshtml
@* ~~@model IEnumerable<MineAsset.Biz.消耗品登記表>~~ *@
@model PagedList.IPagedList<MineAsset.Biz.消耗品登記表>
@using PagedList.Mvc;
<table class="table">
... to render data model ...
</table>
<div class="text-center">
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }),
new PagedListRenderOptions {
DisplayPageCountAndCurrentLocation = true,
PageCountAndCurrentLocationFormat = "第{0}/{1}頁",
})
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment