public ActionResult FilterSearchResults(DisplaySearchResultsViewModel model) { CoreysListEntities db = new CoreysListEntities(); // get a count of the cities int cityCount = model.Cities.Count(); // populate cities dropdown with selected state List <City> citiesList = db.Cities.Where(c => c.StateID == model.SelectedStateIdFilter).ToList(); foreach (City c in citiesList) { SelectListItem newListItem = new SelectListItem(); newListItem.Text = c.CityName; newListItem.Value = c.CityID.ToString(); model.Cities.Add(newListItem); } // first pull all the listings that still match the search term model.Listings = db.Listings.Where(l => l.Headline.Contains(model.SearchTerm) || l.Description.Contains(model.SearchTerm)).ToList(); // if a city was selected if (model.SelectedCityIdFilter > 0) { model.Listings = model.Listings.Where(l => l.CityID == model.SelectedCityIdFilter && l.Price >= model.PriceMinFilter && l.Price <= model.PriceMaxFilter).ToList(); } else if (model.SelectedStateIdFilter > 0) { // else if state was selected but no city model.Listings = model.Listings.Where(l => l.City.StateID == model.SelectedStateIdFilter && l.Price >= model.PriceMinFilter && l.Price <= model.PriceMaxFilter).ToList(); } else { // else just apply price range model.Listings = model.Listings.Where(l => l.Price >= model.PriceMinFilter && l.Price <= model.PriceMaxFilter).ToList(); } // return filtered model with view return(View("DisplaySearchResults", model)); }
// Returns the DisplaySearchResults view with listings that match search term public ActionResult DisplaySearchResults(string searchTerm) { DisplaySearchResultsViewModel searchModel = new DisplaySearchResultsViewModel(searchTerm); return(View("DisplaySearchResults", searchModel)); }