MVC中实现分页

ContractedBlock.gif ExpandedBlockStart.gif Code
 //分页时参数
    public interface IPaginationParameters
    {
        
int? PageNumber { get; }
        
int? PageSize { get; }
    }

    
//排序方向枚举
    public enum SortDirection
    {
        Ascending, 
//升序
        Descending //降序
    }

    
public class SortCondition<T> //排序条件
    {
        
public SortCondition() { }

        
public SortCondition(T sortType, SortDirection sortDirection)
        {
            SortType 
= sortType;
            SortDirection 
= sortDirection;
        }
        
public T SortType { getset; } //排序类型{枚举:实体类为枚举名,属性为枚举值}
        public SortDirection SortDirection { getset; } //升、降序
    }

    
public class SortForm<SortType> //排序条件集合
    {
        
public SortForm() { }

        
public IList<SortCondition<SortType>> SortConditions { getset; }//排序条件集合
    }

    
public class PaginationSortForm<SortType> : SortForm<SortType>, IPaginationParameters
    {
        
public PaginationSortForm()
        {
            PageNumber 
= 1;
            PageSize 
= 15;
        }

        
public PaginationSortForm(int pageNumber, int pageSize)
        {
            PageNumber 
= pageNumber;
            PageSize 
= pageSize;
        }

        
public void Clear()
        {
            PageNumber 
= null;
            PageSize 
= null;
        }

        
public int? PageNumber { getprivate set; }//当前页码
        public int? PageSize { getprivate set; }//每页显示记录数
    }

    
/***********************************************************************/
    
//排序类型{枚举:实体类为枚举名,属性为枚举值}

    
public interface ISortParameters<SortType> //排序条件集合
    {
        IList
<SortCondition<SortType>> SortConditions { get; } //排序条件集合
    }

    
public interface IPaginationSortParameters<SortType> : ISortParameters<SortType>
    {
        
int? StartIndex { get; }
        
int? MaxCount { get; }
    }

    
public class SortParameters<SortType> : ISortParameters<SortType>
    {
        
public IList<SortCondition<SortType>> SortConditions { getset; }
        
//ctor
        public SortParameters(SortType sortType, SortDirection sort)
        {
            
if (SortConditions == null || SortConditions.Count == 0)
            {
                SortConditions 
= new List<SortCondition<SortType>>();
                SortConditions.Add(
new SortCondition<SortType>(sortType, sort));
            }
        }
        
//ctor
        public SortParameters(SortForm<SortType> sortForm, SortType sortType, SortDirection sort)
        {
            
if (sortForm == null)
            {
                sortForm 
= new SortForm<SortType>();
            }
            
this.SortConditions = sortForm.SortConditions;
            
if (SortConditions == null || SortConditions.Count == 0)
            {
                SortConditions 
= new List<SortCondition<SortType>>();
                SortConditions.Add(
new SortCondition<SortType>(sortType, sort));
            }
        }
    }

    
public class PaginationSortParameters<SortType> : SortParameters<SortType>,
                                                      IPaginationSortParameters
<SortType>
    {
        
public PaginationSortParameters(PaginationSortForm<SortType> form,
                                    SortType sortType, SortDirection sort)
            : 
base(form, sortType, sort)
        {
            MaxCount 
= form.PageSize;
            StartIndex 
= (form.PageNumber.Value - 1* form.PageSize.Value;//起始索引
        }

        
public int? StartIndex { getprivate set; }
        
public int? MaxCount { getprivate set; }
    }

转载于:https://www.cnblogs.com/ycdx2001/archive/2009/06/25/1510793.html

你可能感兴趣的:(MVC中实现分页)