asp.net MVC结合EasyUI的datagrid实现分页加载数据

1.引用EasyUI的相关插件,在html文档中编写datagrid,如下:

    
账号 昵称 状态 创建时间
@Html.TextBox("Keyword", "", new { @class = "input-txt-te width-150", @autocomplete = "off" }) 搜索

2.在js中实现加载数据,如下:

 3.调用控制器方法,如下:

(关键:page和rows参数必须是小写的,这是由EasyUI框架自定义决定的)

/// 
        /// 获取员工列表数据
        /// 
        /// 当前第几页
        /// 当前页行数
        /// 
        public ActionResult GetAdminDataGrid(AdminListForm form)
        {
            try
            {
                //获取员工列表的方法,PaginationParameter是自己封装的分页方法,
                //往下看会看到查询的linq的方法,可以不用管PaginationParameter究竟要怎么写
                var adminList = _adminService.GetAdminList(new AdminPara()
                {
                    Keyword = form.Keyword,
                    PaginationParameter = PaginationHelper.GetPagingByDescend(form.page, form.rows),
                });
                List list = new List();
                adminList.Items.ToList().ForEach(item =>
                {
                    list.Add(new
                    {
                        Id = item.Id,
                        LoginName = item.LoginName,
                        Name = item.Name,
                        State = item.State,
                        CreateTime = item.CreateTime.ToyyyyMMddHHmmss()
                    });
                });
                var dataGrid = _commonService.GetGridFormat(adminList.TotalCount, list);
                return Json(dataGrid, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                Log.InfoFormat("获取员工列表数据:{0}", e.Message);
                var dataGrid = _commonService.GetGridFormat(0, "");
                return Json(dataGrid, JsonRequestBehavior.AllowGet);
            }
        } 
  

AdminListForm类:

    public class AdminListForm
    {
        /// 
        /// 关键字(账号或昵称)
        /// 
        public string Keyword { get; set; }
        /// 
        /// 当前页数
        /// 
        public int? page { get; set; }
        /// 
        /// 当前页总行数
        /// 
        public int? rows { get; set; }
    }

查询linq方法:

        /// 
        /// 根据条件获取管理员列表
        /// 
        /// 
        /// 
        public IList GetAdminList(AdminPara adminPara) 
        {
            #region
            //构造查询语句
            var query = (from a in Entities.T_Admins
                         select new AdminInfo()
                         {
                             Id = a.Id,
                             Name = a.Name,
                             LoginName = a.LoginName,
                             Password = a.Password,
                             State = a.State,
                             CreateTime = a.CreateTime,
                         });

            if (!string.IsNullOrEmpty(adminPara.Keyword))
            {
                var keyword = adminPara.Keyword.Trim();
                query = query.Where(a => a.Name.Contains(keyword) || a.LoginName.Contains(keyword));
            }

            var totalCount = query.Select(a => a.Id).LongCount();

            //排序
            query = query.OrderBy(a => a.Id);

            //分页
            if (adminPara.StartIndex.HasValue)
                query = query.Skip(adminPara.StartIndex.Value);
            if (adminPara.MaxCount.HasValue)
                query = query.Take(adminPara.MaxCount.Value);

            var adminList = query.ToList();

            return adminList;
            #endregion
        }

 

 

 

你可能感兴趣的:(easyui,datagrid,mvc)