我有一个简单的分页系统。我在任何地方使用它,只需稍作更改我想制作一个类或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