2011-11-03 53 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它很容易使用,小巧干净。 (当然还有很好的文档和样本)。

相关问题