public ActionResult CreateUpdateCountry(NomCountry model) { if (ModelState.IsValid) { try { var message = ""; if (model.ID > 0) { NomCountry countryDb = db.Countries.FirstOrDefault(x => x.ID == model.ID); countryDb.ID = model.ID; countryDb.Name = model.Name; countryDb.RegionObj = model.RegionObj; countryDb.RegionId = model.RegionId; message = "Successfully edited country!"; db.SaveChanges(); } else { if (db.Countries.Any(x => x.Name.ToLower() == model.Name.ToLower() && x.RegionId == model.RegionId)) { throw new Exception("Country already exists!"); } NomCountry country = new NomCountry(); country.ID = model.ID; country.Name = model.Name; NomRegion region = db.Regions.Find(model.RegionId); country.RegionObj = region; country.RegionId = region.ID; message = "Successfully added country!"; db.Countries.Add(country); db.SaveChanges(); } DisplaySuccessMessage(message); return(Json(true)); } catch (Exception ex) { var modelErrors = new List <string>(); modelErrors.Add(ex.Message); return(Json(modelErrors)); } } else { var errors = GetModelStateErrors(ModelState.Values); return(Json(errors)); } }
public ActionResult DeleteCountry(int id) { NomCountry country = db.Countries.Find(id); db.Countries.Remove(country); db.SaveChanges(); DisplaySuccessMessage("Successfully deleted country!"); return(Json(true)); }
public JsonResult getCountries(int regionId) { NomCountry initialC = new NomCountry(); initialC.ID = -1; initialC.Name = "Entire region"; List <Models.NomCountry> listCountries = new List <Models.NomCountry>(); listCountries.Add(initialC); listCountries.AddRange(db.Countries.Where(c => (c.RegionId.Value == regionId && regionId != -1) || regionId == -1).OrderBy(x => x.Name)); var countries = listCountries.Select(c => new { ID = c.ID, Name = c.Name }); return(Json(countries.ToList(), JsonRequestBehavior.AllowGet)); }
public ActionResult AddEditCountry(int countryId) { List <NomRegion> regions = db.Regions.ToList(); ViewBag.RegionsList = new SelectList(regions, "ID", "Name"); NomCountry model = new NomCountry(); if (countryId > 0) { NomCountry country = db.Countries.Find(countryId); model.ID = country.ID; model.Name = country.Name; model.RegionId = country.RegionId; model.RegionObj = country.RegionObj; } return(PartialView("AddEditCountry", model)); }
protected void AddSearchFields() { NomRegion initial = new NomRegion(); initial.ID = -1; initial.Name = "Select region"; List <Models.NomRegion> list = new List <Models.NomRegion>(); list.Add(initial); list.AddRange(db.Regions); NomCountry initialC = new NomCountry(); initialC.ID = -1; initialC.Name = "Select country/state"; List <Models.NomCountry> listCountries = new List <Models.NomCountry>(); listCountries.Add(initialC); listCountries.AddRange(db.Countries.OrderBy(x => x.Name)); ViewBag.RegionId = new SelectList(list, "ID", "Name"); ViewBag.CountryId = new SelectList(listCountries, "ID", "Name"); }