public ActionResult Create([Bind(Include = "SectorName,SectionID")] Sector sector) { try { if (ModelState.IsValid) { if (!(String.IsNullOrEmpty(sector.SectorName) || db.chkSectorNameExist(sector.SectorName))) { db.Sectors.Add(sector); db.SaveChanges(); return(RedirectToAction("Index")); } else { ViewBag.ErrorValidation = String.IsNullOrEmpty(sector.SectorName) ? "You have not entered a value for Sector" : $"There already exist a Sector named \"{sector.SectorName}\""; } } } catch (Exception ex) { ViewBag.ErrorValidation = $"An error occured while creating this Sector \n{ex.Message}"; } return(View(sector)); }
public ActionResult Edit([Bind(Include = "SectorName,SectionID")] Sector sector, string oldSectorName) { try { if (ModelState.IsValid) { using (var context = new TimesheetEntities()) { Sector sec = context.Sectors.Find(sector.SectionID); if (sec != null) { if (!(String.IsNullOrEmpty(sector.SectorName) || (context.chkSectorNameExist(sector.SectorName) && sec.SectorName != sector.SectorName))) { db.Entry(sector).State = EntityState.Modified; db.SaveChanges(); //db.updateSector(oldSectorName, sector.SectorName); return(RedirectToAction("Index")); } else { ViewBag.ErrorValidation = String.IsNullOrEmpty(sector.SectorName) ? "You have not entered a value for Sector" : $"There already exist a Sector named \"{sector.SectorName}\""; } } else { ViewBag.ErrorValidation = $"This sector ID {sector.SectionID}could not be found in the database"; } } } } catch (Exception ex) { ViewBag.ErrorValidation = $"An error occured during update {ViewBag.oldSectorName}. ({ex.Message})"; } return(View(sector)); }