MVCPager分页使用方法
public ActionResult AdminUserList(UserListModel model) { var pagedList = _userService.SearchAdminUsers(model.PageIndex, model.PageSize, model.Name, model.IsActive); model.Items = new PagedList<UserListItem>(Mapper.Map<List<UserListItem>>(pagedList), pagedList.PageIndex, pagedList.PageSize, pagedList.TotalCount); return View(model); }
public class UserListItem { public Guid Id { get; set; } [Display(Name = "登录帐号")] public string LoginId { get; set; } [Display(Name = "姓名")] public string Name { get; set; } [Display(Name = "电子邮箱")] public string Email { get; set; } [Display(Name = "手机号")] public string Phone { get; set; } [Display(Name = "激活")] public bool IsActive { get; set; } [Display(Name = "注册日期")] public DateTime RegisterDate { get; set; } /// <summary> /// 会员级别 /// </summary> [Display(Name = "会员级别")] public VIPLevel VIPLevel { get; set; } } public class UserListModel : BaseQueryModel { public UserListModel() { IsActive = true; } #region User public IPagedList<UserListItem> Items { get; set; } [Display(Name = "登录帐号")] public string LoginId { get; set; } [Display(Name = "姓名")] public string Name { get; set; } [Display(Name = "手机号")] public string Phone { get; set; } [Display(Name = "激活")] public bool IsActive { get; set; } }
1 public abstract class BaseQueryModel 2 { 3 public BaseQueryModel() 4 { 5 PageIndex = 0; 6 PageSize = 20; 7 } 8 9 public int PageIndex { get; set; } 10 public int PageSize { get; set; } 11 12 public RouteValueDictionary ToParms() 13 { 14 RouteValueDictionary dic = new RouteValueDictionary(); 15 this.GetType().GetProperties().Where(x => x.PropertyType.IsPrimitive 16 || x.PropertyType.IsValueType 17 || (Nullable.GetUnderlyingType(x.PropertyType) != null && (Nullable.GetUnderlyingType(x.PropertyType).IsValueType || Nullable.GetUnderlyingType(x.PropertyType).IsPrimitive)) 18 || x.PropertyType == typeof(string)).ToList().ForEach(x => dic.Add(x.Name, x.GetValue(this))); 19 return dic; 20 } 21 }
public IPagedList<User> SearchUsers(int pageIndex, int pageSize, string name, bool isActive) { var query = this._userRep.Table; if (!string.IsNullOrWhiteSpace(name)) { query = query.Where(x => x.Name == name); } query = query.Where(x => x.IsActive == isActive); query = query.Where(x => x.IsDelete == false); query = query.Where(x => x.IsActive == isActive && x.UserType == UserType.Customer); query = query.OrderByDescending(x => x.RegisterDate).OrderBy(x => x.Name); return new PagedList<User>(query, pageIndex, pageSize); }
引用了MVCPager,DLL地址:http://www.webdiyer.com/aspnetpager/relatedlinks/
有一部分代码是框架封装好的,大家看思路就好。
西安.NET俱乐部群