public IActionResult Index(CountryListViewModel model) { model.Categories = context.Categories.ToList(); model.Games = context.Games.ToList(); var session = new CountrySession(HttpContext.Session); session.SetActiveCat(model.ActiveCat); session.SetActiveGam(model.ActiveGam); // if no count value in session, use data in cookie to restore fave teams in session int?count = session.GetMyCountryCount(); if (count == null) { var cookies = new CountryCookies(Request.Cookies); string[] ids = cookies.GetMyCountryIds(); List <Country> mycountries = new List <Country>(); if (ids.Length > 0) { mycountries = context.Countries.Include(t => t.Category) .Include(t => t.Game) .Where(t => ids.Contains(t.CountryID)).ToList(); } session.SetMyCountries(mycountries); } IQueryable <Country> query = context.Countries; if (model.ActiveCat != "all") { query = query.Where( t => t.Category.CategoryID.ToLower() == model.ActiveCat.ToLower()); } if (model.ActiveGam != "all") { query = query.Where( t => t.Game.GameID.ToLower() == model.ActiveGam.ToLower()); } model.Countries = query.ToList(); return(View(model)); }