Donate

ASP.NET MVC Implementing MVCContribGrid CustomPagination<T> Class

Out of boredom, I came upon a class named CustomPagination in MVCContrib that that implements IPagination, IEnumerable
and other interfaces that you can customize your paging needs.Here's the class definition:
namespace MvcContrib.Pagination  
 {  
   public class CustomPagination<T> : IPagination<T>, IPagination, IEnumerable<T>, IEnumerable  
   {  
     public CustomPagination(IEnumerable<T> dataSource, int pageNumber, int pageSize, int totalItems);  
     public int FirstItem { get; }  
     public bool HasNextPage { get; }  
     public bool HasPreviousPage { get; }  
     public int LastItem { get; }  
     public int PageNumber { get; }  
     public int PageSize { get; }  
     public int TotalItems { get; }  
     public int TotalPages { get; }  
     public IEnumerator<T> GetEnumerator();  
   }  
 }
I found an article in this blog(http://lsd.luminis.eu) on how to use the CustomPagination class that returns an Enumerable object with paging but without implementations of the CusomPagination class.

After googling around, I found Jeremy Skinner's class implementation of CusomPagination.
namespace MvcContrib.Pagination  
 {  
     /// <summary>  
     /// Implementation of IPagination that wraps a pre-paged data source.   
     /// </summary>  
     public class CustomPagination<T> : IPagination<T>  
     {  
         private readonly IList<T> _dataSource;  
         /// <summary>  
         /// Creates a new instance of CustomPagination  
         /// </summary>  
         /// <param name="dataSource">A pre-paged slice of data</param>  
         /// <param name="pageNumber">The current page number</param>  
         /// <param name="pageSize">The number of items per page</param>  
         /// <param name="totalItems">The total number of items in the overall datasource</param>  
         public CustomPagination(IEnumerable<T> dataSource, int pageNumber, int pageSize, int totalItems)  
         {  
             _dataSource = dataSource.ToList();  
             PageNumber = pageNumber;  
             PageSize = pageSize;  
             TotalItems = totalItems;  
         }  
         public IEnumerator<T> GetEnumerator()  
         {  
             return _dataSource.GetEnumerator();  
         }  
         IEnumerator IEnumerable.GetEnumerator()  
         {  
             return GetEnumerator();  
         }  
         public int PageNumber { get; private set; }  
         public int PageSize { get; private set; }  
         public int TotalItems { get; private set; }  
         public int TotalPages  
         {  
             get { return (int)Math.Ceiling(((double)TotalItems) / PageSize); }  
         }  
         public int FirstItem  
         {  
             get  
             {  
                 return ((PageNumber - 1) * PageSize) + 1;  
             }  
         }  
         public int LastItem  
         {  
             get { return FirstItem + _dataSource.Count - 1; }  
         }  
         public bool HasPreviousPage  
         {  
             get { return PageNumber > 1; }  
         }  
         public bool HasNextPage  
         {  
             get { return PageNumber < TotalPages; }  
         }  
     }  
 }  
Based from this implementation, I added some features to this class and declared it in my Model. An implementation of the LastItem property is shown below based on my needs.
 get  
       {  
         if (PageNumber == TotalPages)  
           return TotalItems;  
         else  
           return (PageNumber * PageSize);  
       }  
After customizing the class, you can use it in your controller like this:
IPagination pm = new DisplayingAGrid.ModelsCustomPagination<Product>(products.Cast<Product>(),        
               p, 10, products.Cast<Product>().ToList().Count);  
  ViewData["pagedData"] = pm;  
  return View();
And in your ASP.NET MVC View, you can apply it in your MVCContribGrid:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
   <h2>Products Grid with Custom Paging</h2>  
   <%: Html.Grid((IEnumerable<Product>)ViewData["pagedData"]).Columns(col =>   
         {   
           col.For(p => p.ProductName)   
            .Named("Product")   
            .Attributes(@class => "left");   
           col.For(p => p.Category.CategoryName)   
            .Named("Category")   
            .Attributes(@class => "left");   
           col.For(p => p.QuantityPerUnit)   
            .Named("Qty/Unit")   
            .Attributes(@class => "left");   
           col.For(p => p.UnitPrice)   
            .Named("Price")   
            .Format("{0:c}")   
            .Attributes(@class => "right");   
           col.For(p => p.Discontinued ? string.Format(@"<img src=""{0}"" />", Url.Content("~/Content/cancel.png")) : string.Empty).DoNotEncode().Named("Discontinued");   
          }) %>    
   <%= Html.Pager((IPagination) (ViewData["pagedData"] as IEnumerable<Product>)).First("<<").Last(">>").Next(">").Previous("<").Format("Item {0} - {1} of {2}")%>    
 </asp:Content>  
Here's the rendered ASP.NET MVC Page with MVCContribgrid:
ASP.NET MVC Implementing MVCContribGrid CustomPagination Class

Greg :)

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.