2011-11-03 66 views

回答

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

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

如何小號控制器應該如何? –

0

對於分頁結果我使用PagedList它很容易使用,小巧幹淨。 (當然還有很好的文檔和樣本)。

相關問題