Пример #1
0
        // GET: Users
        public async Task <IActionResult> Index(int?position, int?stat, string name, SortState sortOrder = SortState.NameAsc, int page = 1)
        {
            int pageSize = 3;

            IQueryable <User> users = _context.Users
                                      .Include(u => u.Position)
                                      .Include(x => x.Status)
                                      .OrderBy(u => u.ID)
                                      .Skip((page - 1) * pageSize)
                                      .Take(pageSize);


            if (position != null && position != 0)
            {
                users = users.Where(p => p.PositionId == position);
            }

            if (stat != null && stat != 0)
            {
                users = users.Where(p => p.StatusId == stat);
            }

            if (!String.IsNullOrEmpty(name))
            {
                users = users.Where(p => p.Name.Contains(name));
            }

            users = sortOrder switch
            {
                SortState.NameDesc => users.OrderByDescending(s => s.Name),
                SortState.SurNameAsc => users.OrderBy(s => s.Surname),
                SortState.SurNameDesc => users.OrderByDescending(s => s.Surname),
                _ => users.OrderBy(s => s.Name),
            };

            var count = await _context.Users.CountAsync();

            var intems = await users.ToListAsync();

            Pagination pagination = new Pagination
            {
                PageItemsAmount       = pageSize,
                CurrentPage           = page,
                ControllerName        = "Users",
                ShowLastAndFirstPages = true,
                ActionName            = "Index",
                RouteParams           = new Dictionary <string, object> {
                    { "section", "TV" }
                },
                Params = new Dictionary <string, object> {
                    { "HDTV", "yes" }
                }
            };

            pagination.ItemsAmount = count;
            pagination.Refresh();

            IndexViewModel viewModel = new IndexViewModel
            {
                PaginIndexViewModel = new PaginIndexViewModel(count, page, pageSize),
                SortViewModel       = new SortViewModel(sortOrder),
                Pagination          = pagination,
                FilterViewModel     = new FilterViewModel(_context.Positions.ToList(), position, _context.Statuses.ToList(), stat, name),
                Users = intems,
            };

            return(View(viewModel));
        }