protected void btnSave_Click(object sender, EventArgs e) { ctlAdminMaster.ErrorMessage = ""; if (cityNameValidator.IsValid) { if (txtCrCity.Text.Trim().Length == 0) { ctlAdminMaster.ErrorMessage = "City Name Can't be Empty"; txtCrCity.Focus(); } else if (dpStateCity.Text.Equals("None") == true) { ctlAdminMaster.ErrorMessage = "Select the State"; dpStateCity.Focus(); } else { string CityName = txtCrCity.Text; State _state = new State(); _state.StateId = long.Parse(dpStateCity.SelectedItem.Value); City _city = new City(); _city.Name = CityName; _city.StateInfo = _state; ICityManager cityManger = (ICityManager)BusinessObjectManager.GetCityManager(); try { if (cityManger.AddCity(_city)) { ctlAdminMaster.ErrorMessage = "City Added Successfully"; } else { ctlAdminMaster.ErrorMessage = "City already exists"; } } catch (CityManagerException ex) { ctlAdminMaster.ErrorMessage = ex.Message; } } } }
public ActionResult Create(City city) { ViewBag.StateId = new SelectList(cityMgr.GetAllState(), "StateId", "StateName"); if (ModelState.IsValid) { City DuplicateCity = cityMgr.GetCity().FirstOrDefault(a => city.CityName.ToLower() == a.CityName.ToLower()); if (DuplicateCity == null) { cityMgr.AddCity(city); return(RedirectToAction("Index")); } else { ModelState.AddModelError("", "City Name Already Exists"); } } return(View()); }