public IActionResult Index(string filter, int page = 1, int? houseType = null, int? cityId = null, int? districtId = null, int? minCost = null, int? maxCost = null, int? objectId = null, bool? isArchive = null) { var typesHousings = _context.TypesHousing.ToList(); if (User.IsInRole(RoleNames.Employee)) { cityId = CurrentUser?.City?.Id; } var filterData = new HousingExtensions.FilterParams() { CityId = cityId, PriceTo = minCost, PriceFrom = maxCost, Page = page, IsArchived = isArchive }; if (houseType.HasValue) { filterData.HouseTypeId = new int[] { houseType.Value }; } var query = _context.Housing.Where(x => HousingExtensions.Filter(filterData)(x)); int totalPages; int totalItems; var queryResult = query.GetPage(page, out totalItems, out totalPages); var items = queryResult.Select(x => HousingEditModel.Create(x, typesHousings, User)).ToList(); ViewBag.TotalItems = _context.Housing.Count(); ViewBag.FilteredItemsCount = totalItems; var model = new HousingIndexModel { Items = items, Filters = new HousingIndexFilterModel { IsArchived = isArchive ?? false, HousingTypeId = houseType ?? 0, CityId = cityId ?? 0, DistrictId = districtId ?? 0 }, TotalPages = totalPages, CurrentPage = page }; return View(model); }
public IActionResult Filter(HousingIndexModel model, int page = 1) { var filters = model.Filters; if (filters == null) { return RedirectToAction("Index", new { page }); } Func<int?, int?> intOrNull = (value) => { if (value.HasValue && value.Value > 0) { return value; } return null; }; return RedirectToAction("Index", new { page, houseType = intOrNull(filters.HousingTypeId), cityId = intOrNull(filters.CityId), districtId = intOrNull(filters.DistrictId), minCost = intOrNull(filters.MinCost), maxCost = intOrNull(filters.MaxCost), objectId = intOrNull(filters.SelectedObjectId), isArchive = filters.IsArchived }); }