//根据查询条件获得分页数据 private IEnumerable <CarCategory> LoadPageCarCategoryData(CarCategoryQueryParams param, out int total) { var allCarCategories = CarCategoryRepository.LoadEntities( c => c.Status == (short)StatusEnum.Enable && c.DelFlag == (short)DelFlagEnum.Normal); if (!string.IsNullOrEmpty(param.Name)) { allCarCategories = allCarCategories.Where(c => c.Name.Contains(param.Name)); } if (!string.IsNullOrEmpty(param.JoinStartTime.ToString()) && !string.IsNullOrEmpty(param.JoinEndTime.ToString())) { allCarCategories = allCarCategories.Where(c => c.SubTime >= param.JoinStartTime && c.SubTime <= param.JoinEndTime); } total = allCarCategories.Count(); IEnumerable <CarCategory> result = allCarCategories .OrderByDescending(c => c.ID) .Skip(param.PageSize * (param.PageIndex - 1)) .Take(param.PageSize); return(result); }
//显示所有分类并考虑查询和分页 public ActionResult GetCarCategoryJson() { //获取datagrid传来的2个参数 int pageIndex = int.Parse(Request["page"]); int pageSize = int.Parse(Request["rows"]); //获取搜索参数 if (!string.IsNullOrEmpty(Request["txtName"])) { txtName = Request["txtName"]; } if (!string.IsNullOrEmpty(Request["startSubTime"])) { startSubTime = DateTime.Parse(Request["startSubTime"]); } if (!string.IsNullOrEmpty(Request["endSubTime"])) { endSubTime = DateTime.Parse(Request["endSubTime"]); } //初始化查询实例 var temp = new CarCategoryQueryParams { PageIndex = pageIndex, PageSize = pageSize, Name = txtName, JoinEndTime = endSubTime, JoinStartTime = startSubTime }; //获取所有满足条件的数据,并获得总记录数 int totalNum = 0; var allCarCategory = LoadPageCarCategoryData(temp, out totalNum); //投影出需要传递到前台的数据 var result = from c in allCarCategory select new { c.ID, c.Name, c.IsLeaf, c.Level, c.ParentID, c.PreLetter, c.Status, c.DelFlag, c.SubTime }; //构建datagrid所需要的json格式 var jsonResult = new { total = totalNum, rows = result }; return(Json(jsonResult, JsonRequestBehavior.AllowGet)); }