0
Mine是一个asp.net mvc应用程序。我有一个超过25行的索引视图。这使得我的页面渲染缓慢。我需要在这里实施分页。我想限制行数为6左右。请有人可以帮我解决这个问题吗?在asp.net中为我的索引视图添加分页mvc
Mine是一个asp.net mvc应用程序。我有一个超过25行的索引视图。这使得我的页面渲染缓慢。我需要在这里实施分页。我想限制行数为6左右。请有人可以帮我解决这个问题吗?在asp.net中为我的索引视图添加分页mvc
型号:
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它很容易使用,小巧干净。 (当然还有很好的文档和样本)。
如何小号控制器应该如何? –