public ActionResult Edit([Bind(Include = "LocationID,LocationName,SectionID")] Location location) { try { if (ModelState.IsValid) { using (var context = new TimesheetEntities()) { Location loc = context.Locations.Find(location.LocationID); if (loc != null) { if (!(String.IsNullOrEmpty(location.LocationName) || (context.chkLocationNameExist(location.LocationName) && loc.LocationName != location.LocationName))) { db.Entry(location).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } else { ViewBag.ValLocationName = String.IsNullOrEmpty(location.LocationName) ? "You have not entered a value for Location" : $"There already exist a Location named \"{location.LocationName}\""; } } else { ViewBag.LocationName = $"This location could not be found in the database"; } } } ViewBag.SectionID = new SelectList(db.Sectors, "SectorName", "SectorName", location.SectionID); } catch (Exception ex) { ViewBag.SectionID = new SelectList(db.Sectors, "SectorName", "SectorName", location.SectionID); ViewBag.ValLocationName = $"An error occured while updating this Location \n{ex.Message}"; } return(View(location)); }
public ActionResult Create([Bind(Include = "LocationID,LocationName,SectionID")] Location location) { try { ViewBag.strSlectedSector = location.SectionID; if (ModelState.IsValid) { if (!String.IsNullOrEmpty(location.LocationName)) { if (!(String.IsNullOrEmpty(location.LocationName) || db.chkLocationNameExist(location.LocationName))) { db.Locations.Add(location); db.SaveChanges(); return(RedirectToAction("Index")); } else { ViewBag.ValLocationName = String.IsNullOrEmpty(location.LocationName) ? "You have not entered a value for Location" : $"There already exist a Location named \"{location.LocationName}\""; } } else { ViewBag.ValSectorName = "Select a value for Sector Name"; } } ViewBag.SectionID = new SelectList(db.Sectors, "SectionID", "SectorName", location.SectionID); } catch (Exception ex) { ViewBag.SectionID = new SelectList(db.Sectors, "SectionID", "SectorName", location.SectionID); ViewBag.ValSummary = $"An error occured while creating this Location \n{ex.Message}"; } return(View(location)); }