2011-11-03 2 views
0

Mine은 asp.net mvc 응용 프로그램입니다. 행이 25 이상인 인덱스보기가 있습니다. 이로 인해 내 페이지가 느리게 렌더링됩니다. 이 페이지 매김을 구현해야합니다. 행의 수를 6 정도로 제한하고 싶습니다. 누군가 나를 도와 줄 수 있습니까?asp.net mvc 내 인덱스보기에 페이지 매김 추가

답변

0

모델 :

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); } 
     } 
    } 
} 

Html 헬퍼 :

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> 
+0

방법들 컨트롤러가 있어야할까요? –

0

페이징 결과에 대해서는 PagedList을 사용하면 작고 깨끗합니다. (물론 좋은 문서와 샘플이 있습니다).