Last active
March 1, 2021 09:50
-
-
Save relyky/b8dfa47b864121c3aaf5f74b09b84fa1 to your computer and use it in GitHub Desktop.
MVC5 Pagination, PagedList.Mvc, 分頁
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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