public ActionResult Index(string cityName , int? countryId) { //ViewBag.CountryId = new SelectList(db.Acountry, "CountryId", "CountryName"); //var acity = db.Acity.Where(aCity => (aCity.CityName == cityName)).ToList(); //return View(acity); // another way ............. if (!String.IsNullOrEmpty(cityName)) { ViewBag.CountryId = new SelectList(db.Acountry, "CountryId", "CountryName"); DatabaseContext myDB = new DatabaseContext(); var City = from city in myDB.Acity where city.CityName == cityName select city; return View(City.ToList()); } else { ViewBag.CountryId = new SelectList(db.Acountry, "CountryId", "CountryName"); DatabaseContext myDB = new DatabaseContext(); var City = from country in myDB.Acity where (int) country.CountryId == countryId select country; return View(City.ToList()); } }
public ActionResult Index(int? countryId) { // int count = 0; DatabaseContext myDB = new DatabaseContext(); ViewBag.CountryId = new SelectList(db.Acountry, "CountryId", "CountryName"); var city = (from aCountry in myDB.Acity where (int) aCountry.CountryId == countryId select aCountry); // city.Count(); // city.Count() return View(city.ToList()); }