Exemplo n.º 1
0
        public async Task <ActionResult> Index(int?page, int?albumId, string keyword)
        {
            var vm = new PhotoListVM()
            {
                AlbumId   = albumId,
                Keyword   = keyword,
                PageIndex = page ?? 1,
                PageSize  = SettingsManager.Photo.PageSize
            };
            var query = _db.Photos.Include(d => d.Album).AsQueryable();

            if (albumId > 0)
            {
                query = query.Where(d => d.AlbumId == albumId);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(d => d.Title.Contains(keyword));
            }

            var pagelist = await query.OrderByDescending(d => d.Id).Skip((vm.PageIndex - 1) * vm.PageSize).Take(vm.PageSize).ProjectTo <PhotoVM>().ToListAsync();


            vm.TotalCount = await query.CountAsync();

            vm.Photos = new StaticPagedList <PhotoVM>(pagelist, vm.PageIndex, vm.PageSize, vm.TotalCount);

            ViewBag.PageSizes = new SelectList(Site.PageSizes());

            var categoryList = await _db.Albums.OrderByDescending(c => c.Importance).ToListAsync();

            ViewBag.Categories = new SelectList(categoryList, "Id", "Title");

            return(View(vm));
        }
Exemplo n.º 2
0
        // GET: Admin/Photos
        public async Task <IActionResult> Index(string keyword, string orderby, string sort, int?albumId, int?page)
        {
            var vm = new PhotoListVM()
            {
                PageIndex = page == null || page <= 0 ? 1 : page.Value,
                Keyword   = keyword,
                AlbumId   = albumId,
                PageSize  = SettingsManager.Photo.PageSize,
                OrderBy   = orderby,
                Sort      = sort
            };

            //var pageSize = SettingsManager.Photo.PageSize;
            var query = _context.Photos.Include(d => d.Album).AsNoTracking().AsQueryable();

            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(d => d.Title.Contains(keyword));
            }

            if (albumId > 0)
            {
                query = query.Where(d => d.AlbumId == albumId);
            }


            var gosort = $"{orderby}_{sort}";

            query = gosort switch
            {
                "importance_asc" => query.OrderBy(s => s.Importance),
                "importance_desc" => query.OrderByDescending(s => s.Importance),
                "title_asc" => query.OrderBy(s => s.Title),
                "title_desc" => query.OrderByDescending(s => s.Title),
                "date_asc" => query.OrderBy(s => s.CreatedDate),
                "date_desc" => query.OrderByDescending(s => s.CreatedDate),

                _ => query.OrderByDescending(s => s.Id),
            };


            vm.TotalCount = await query.CountAsync();

            var clients = await query
                          .Skip((vm.PageIndex - 1) *vm.PageSize).Take(vm.PageSize).ProjectTo <PhotoBVM>(_mapper.ConfigurationProvider).ToListAsync();


            vm.Photos = new StaticPagedList <PhotoBVM>(clients, vm.PageIndex, vm.PageSize, vm.TotalCount);

            var categories = await _context.Albums.AsNoTracking()
                             .OrderByDescending(d => d.Importance).ToListAsync();

            ViewData["Categories"] = new SelectList(categories, "Id", "Title");

            ViewBag.PageSizes = new SelectList(Site.PageSizes());

            return(View(vm));
        }