我有一個簡單的分頁系統。我在任何地方使用它,只需稍作更改我想製作一個類或dll文件並更一般地使用它。我不想重複自己。分頁類DLL - 不要重複自己
我的控制器:
public ActionResult Index(int sayfa = 1)
{
int totalPage = Posts.Count();
int postPerPage = 10;
int pageCount = totalPage/postPerPage;
if ((totalPage % postPerPage) > 0)
{
pageCount++;
}
ViewBag.PageCurrent = sayfa;
ViewBag.PageCount = pageCount;
Posts = (IOrderedQueryable<Post>)Posts.Skip(postPerPage * (sayfa - 1)).Take(postPerPage);
return View(Posts.ToList());
}
我的觀點:
<div class="pagination">
@{
int pageCurrent = ViewBag.PageCurrent;
int pageCount = ViewBag.PageCount;
if (pageCurrent != 1)
{
@Html.ActionLink(" ", "Index", "Post", new { sayfa = pageCurrent - 1 }, new { @class = "previous" })
}
for (int i = 1; i <= pageCount; i++)
{
if (i == pageCurrent)
{
<a href="@Url.Action("Index", "Post", new { sayfa = i, })" style="background-color: #af3425">@i</a>
}
else
{
<a href="@Url.Action("Index", "Post", new { sayfa = i })">@i</a>
}
}
if (pageCurrent != pageCount)
{
@Html.ActionLink(" ", "Index", "Post", new { sayfa = pageCurrent + 1 }, new { @class = "next" })
}
}
<br class="clear" />
</div>
可能的變量,我可以在控制器改變:
int postPerPage = 10; (This is not so important)
(Must-Have Changeable):
Posts = (IOrderedQueryable<Post>)Posts.Skip(postPerPage * (sayfa - 1)).Take(postPerPage);
return View(Posts.ToList());
可能的變量,我可以在視圖改變:
@Url.Action("Index", "Post" .... (This is also must-have)
我期望的使用:
IN CONTROLLER:
Posts = Posts.ToMyCustomPagerClass()
IN VIEW:
@Html.Pagination()
我怎麼能這樣做? 現在我創建了下面的類,但ViewBags是有問題的。錯誤:「ViewBag」這個名字不會在目前情況下
public static class ePager<T>
{
public static IOrderedQueryable<T> ePagedList(IOrderedQueryable<T> sinif, int page, int itemPerPage = 10)
{
int totalPage = sinif.Count();
int pageCount = totalPage/itemPerPage;
if ((totalPage % itemPerPage) > 0)
{
pageCount++;
}
ViewBag.PageCurrent = page;
ViewBag.PageCount = pageCount;
sinif = (IOrderedQueryable<T>)sinif.Skip(itemPerPage * (page - 1)).Take(itemPerPage);
return sinif;
}
}
我想讓這段代碼可重用。每次我需要「複製粘貼和編輯」。我想用這個代碼。在CONTROLLER Posts = Posts.ToMyCustomPagerClass()和VIEW @ Html.Pagination()中。類似的東西 – Sacmalama