【发布时间】:2014-01-07 15:47:36
【问题描述】:
我的是一个 asp.net mvc 应用程序。我有一个超过 25 行的索引视图。这使我的页面渲染速度变慢。我需要在此实现分页。我想将行数限制为 6 左右。请问有人可以帮我吗?
【问题讨论】:
标签: jquery asp.net-mvc pagination
我的是一个 asp.net mvc 应用程序。我有一个超过 25 行的索引视图。这使我的页面渲染速度变慢。我需要在此实现分页。我想将行数限制为 6 左右。请问有人可以帮我吗?
【问题讨论】:
标签: jquery asp.net-mvc pagination
型号:
namespace Comtesys.WebUI.Models
{
public class PagingInfo
{
public int TotalItems { get; set; }
public int ItemsPerPage { get; set; }
public int CurrentPage { get; set; }
public int TotalPages
{
get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
}
}
}
HtmlHelper:
namespace Comtesys.WebUI.HtmlHelpers
{
public static class PagingHelpers
{
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= pagingInfo.TotalPages; i++)
{
TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == pagingInfo.CurrentPage)
tag.AddCssClass("selected");
result.AppendLine(tag.ToString());
}
return MvcHtmlString.Create(result.ToString());
}
}
}
控制器:
public ActionResult List(int page = 1)
{
public int PageSize = 6;
var viewModel = new YourModel
{
ListItems = _repository.SomeCollection,
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = _repository.SomeCollection.Count()
}
};
return View(viewModel);
}
使用:
<div class="pager">
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x }))
</div>
【讨论】:
对于分页结果,我使用PagedList,它易于使用、小巧且干净。 (当然还有很好的文档和示例)。
【讨论】: