public IActionResult Index(CountryListViewModel model) { var session = new CountrySession(HttpContext.Session); session.SetActiveGame(model.ActiveGame); session.SetActiveSport(model.ActiveSport); session.SetActiveCategory(model.ActiveCategory); // if no count value in session, use data in cookie to restore fave Countries in session int?count = session.GetMyCountryCount(); if (count == null) { var cookies = new CountryCookies(HttpContext.Request.Cookies); string[] ids = cookies.GetMyCountryIds(); List <Country> myCountries = new List <Country>(); if (ids.Length > 0) { myCountries = context.Countries.Include(c => c.Game) .Include(c => c.Sport) .Include(c => c.Category) .Where(c => ids.Contains(c.CountryId)).ToList(); } session.SetMyCountries(myCountries); } model.Games = context.Games.ToList(); model.Sports = context.Sports.ToList(); model.Categories = context.Categories.ToList(); IQueryable <Country> query = context.Countries; if (model.ActiveGame != "all") { query = query.Where( c => c.Game.GameId.ToLower() == model.ActiveGame.ToLower()); } if (model.ActiveSport != "all") { query = query.Where( c => c.Sport.SportId.ToLower() == model.ActiveSport.ToLower()); } if (model.ActiveCategory != "all") { query = query.Where( c => c.Category.CategoryId.ToLower() == model.ActiveCategory.ToLower()); } model.Countries = query.ToList(); return(View(model)); }