// GET: Admin/Documents public async Task <IActionResult> Index(string keyword, string orderby, string sort, int?categoryId, int?page) { var vm = new DocumentListVM() { PageIndex = page == null || page <= 0 ? 1 : page.Value, Keyword = keyword, CategoryId = categoryId, PageSize = 10, OrderBy = orderby, Sort = sort }; //var pageSize = SettingsManager.Document.PageSize; var query = _context.Documents.Include(d => d.Category).AsNoTracking().AsQueryable(); if (!string.IsNullOrEmpty(keyword)) { query = query.Where(d => d.Title.Contains(keyword)); } if (categoryId > 0) { query = query.Where(d => d.CategoryId == categoryId); } var gosort = $"{orderby}_{sort}"; query = gosort switch { "view_asc" => query.OrderBy(s => s.DownloadCount), "view_desc" => query.OrderByDescending(s => s.DownloadCount), "title_asc" => query.OrderBy(s => s.Title), "title_desc" => query.OrderByDescending(s => s.Title), "date_asc" => query.OrderBy(s => s.Pubdate), "date_desc" => query.OrderByDescending(s => s.Pubdate), _ => query.OrderByDescending(s => s.Id), }; vm.TotalCount = await query.CountAsync(); var clients = await query .Skip((vm.PageIndex - 1) *vm.PageSize).Take(vm.PageSize).ProjectTo <DocumentBVM>(_mapper.ConfigurationProvider).ToListAsync(); vm.Documents = new StaticPagedList <DocumentBVM>(clients, vm.PageIndex, vm.PageSize, vm.TotalCount); var categories = await _context.DocCategories.AsNoTracking() .OrderByDescending(d => d.Importance).ToListAsync(); ViewData["Categories"] = new SelectList(categories, "Id", "Title"); ViewBag.PageSizes = new SelectList(Site.PageSizes()); return(View(vm)); }
public async System.Threading.Tasks.Task <ActionResult> Index(int?page, int?categoryId, string keyword) { var vm = new DocumentListVM { CategoryId = categoryId ?? 0, PageIndex = (page ?? 1), PageSize = SettingsManager.Doc.PageSize, Keyword = keyword }; var query = _db.Documents.AsQueryable(); if (categoryId > 0) { query = query.Where(d => d.CategoryId == categoryId); } if (!string.IsNullOrEmpty(keyword)) { query = query.Where(d => d.Title.Contains(keyword)); } var list = await query.OrderByDescending(d => d.Importance).ThenByDescending(d => d.Id) .Skip((vm.PageIndex - 1) * vm.PageSize).Take(vm.PageSize).ToListAsync(); vm.TotalCount = await query.CountAsync(); var categoryList = await _db.DocumentCategories.OrderByDescending(c => c.Importance).ToListAsync(); var categories = new SelectList(categoryList, "Id", "Title"); ViewBag.Categories = categories; vm.Documents = new StaticPagedList <Document>(list, vm.PageIndex, vm.PageSize, vm.TotalCount); ViewBag.PageSizes = new SelectList(Site.PageSizes()); return(View(vm)); }