public ActionResult Index(_Pager pager) { IQueryable <Post> posts = _context.Posts.Include(x => x.Comments); int pageIndex = pager.PageIndex, pageSize = pager.PageSize; string searchValue = pager.SearchValue, sortFieldValue = $"{nameof(Property.Name)}"; //default order field string orderByValue = "asc"; //default order by if (!string.IsNullOrEmpty(pager.SortField)) { string[] segments = pager.SortField.Split('_'); sortFieldValue = segments[0]; orderByValue = segments[1] == "asc" ? "asc" : "desc"; }//end if check is sort field has value posts = posts.Where(x => string.IsNullOrEmpty(searchValue) || x.Name.Contains(searchValue)); int totalPages = (int)Math.Ceiling(posts.Count() / (double)pageSize); ViewBag.SortField = pager.SortField; ViewBag.PageIndex = pageIndex; ViewBag.PageSize = pageSize; ViewBag.TotalPages = totalPages; ViewBag.SearchValue = searchValue; return(View(_converter.CreatePostViewModels(posts.OrderByPropertyName(sortFieldValue, orderByValue) .Skip((pageIndex - 1) * pageSize) .Take(pageSize).ToList()))); }
public ActionResult Index(int?typeId, _Pager pager) { IQueryable <Comment> comments = _context.Comments; if (typeId.HasValue) { switch (typeId.Value) { case 1: comments = comments.Where(x => x.PropertyId.HasValue); break; case 2: comments = comments.Where(x => x.PostId.HasValue); break; } } int pageIndex = pager.PageIndex, pageSize = pager.PageSize; string sortFieldValue = $"{nameof(Comment.Owner)}"; //default order field string orderByValue = "asc"; //default order by if (!string.IsNullOrEmpty(pager.SortField)) { string[] segments = pager.SortField.Split('_'); sortFieldValue = segments[0]; orderByValue = segments[1] == "asc" ? "asc" : "desc"; }//end if check is sort field has value int totalPages = (int)Math.Ceiling(comments.Count() / (double)pageSize); ViewBag.SortField = pager.SortField; ViewBag.PageIndex = pageIndex; ViewBag.PageSize = pageSize; ViewBag.TotalPages = totalPages; ViewBag.SearchValue = typeId; return(View(_converter.CreateReadCommentViewModels(comments.OrderByPropertyName(sortFieldValue, orderByValue) .Skip((pageIndex - 1) * pageSize) .Take(pageSize).ToList(), _rootUrl))); }