public ActionResult Edit(LocPhoneNums locPhoneNums) { /***** Logging initial settings *****/ DbChangeLog log = new DbChangeLog(); log.UserName = User.Identity.Name; log.Controller = "LocPhoneNums"; log.Action = (locPhoneNums.Id != 0) ? "Edit" : "Create"; log.ItemId = locPhoneNums.Id; // if this is an edit to an exhisting item, record the old item to the log if (log.Action == "Edit") { LocPhoneNums oldPhone = context.LocPhoneNums.FirstOrDefault(c => c.Id == locPhoneNums.Id); log.BeforeChange = Domain.Extensions.DbLogExtensions.LocPhoneToString(oldPhone); } // record the newly attempted change log.AfterChange = Domain.Extensions.DbLogExtensions.LocPhoneToString(locPhoneNums); /***** end Logging initial settings *****/ if (ModelState.IsValid) { try { context.SaveLocPhoneNum(locPhoneNums); // need to record the id here, if this item has just been created it will not have an ID until it has been recorded to the DB log.ItemId = locPhoneNums.Id; log.Success = true; TempData["message"] = string.Format("{0} has been saved", locPhoneNums.Name); } catch (Exception e) { log.Error = e.ToString(); log.Success = false; TempData["alert"] = "There has been an error. That item has not been saved"; } } else { TempData["alert"] = string.Format("{0} has not been saved", locPhoneNums.Name); // record the errors and error status to the log log.Success = false; log.Error = "Errors: "; foreach (ModelState modelState in ViewData.ModelState.Values) { foreach (ModelError error in modelState.Errors) { log.Error += error + "<br />"; } } } context.SaveLog(log); return(RedirectToAction("Edit", new { Id = locPhoneNums.Id })); }
public ActionResult Delete(int id) { DbChangeLog log = new DbChangeLog(); LocPhoneNums deletedPhone = context.DeleteLocPhoneNum(id); log.UserName = User.Identity.Name; log.Controller = ControllerContext.RouteData.Values["controller"].ToString(); log.Action = ControllerContext.RouteData.Values["action"].ToString(); log.ItemId = id; log.BeforeChange = Domain.Extensions.DbLogExtensions.LocPhoneToString(deletedPhone); if (deletedPhone != null) { log.Success = true; TempData["message"] = string.Format("{0} was deleted", deletedPhone.Name); } else { log.Success = false; log.Error = "Unable to delete location"; TempData["alert"] = "Sorry, there was an error, that location has not been deleted"; } context.SaveLog(log); return(RedirectToAction("Edit", "Locations", new { id = deletedPhone.LocationId })); }
public static string LocPhoneToString(LocPhoneNums phone) { string locString = "<table class='table table-striped'>"; locString += "<tr><th>ID:</th><td>" + phone.Id.ToString() + "</td></tr>"; locString += "<tr><th>Name:</th><td>" + phone.Name + "</td></tr>"; locString += "<tr><th>Number:</th><td>" + phone.Number + "</td></tr>"; locString += "<tr><th>Location ID:</th><td>" + phone.LocationId + "</td></tr>"; locString += "</table>"; return(locString); }
public LocPhoneNums DeleteLocPhoneNum(int id) { LocPhoneNums dbEntry = context.LocPhoneNums.Find(id); if (dbEntry != null) { context.LocPhoneNums.Remove(dbEntry); context.SaveChanges(); SaveLocPhonesXMLFile(); } return(dbEntry); }
// GET: LocPhoneExts/Create public ActionResult Manage(int id) { LocPhoneNums phoneNums = context.LocPhoneNums.FirstOrDefault(p => p.Id == id); LocationPhoneExtensionsViewModel model = new LocationPhoneExtensionsViewModel { PhoneNums = phoneNums, Location = context.Locations .FirstOrDefault(l => l.Id == phoneNums.LocationId), LocPhoneExts = new LocPhoneExts() }; return(View(model)); }
public void SaveLocPhoneNum(LocPhoneNums phone) { if (phone.Id == 0) { context.LocPhoneNums.Add(phone); } else { LocPhoneNums dbEntry = context.LocPhoneNums.Find(phone.Id); if (dbEntry != null) { dbEntry.Name = phone.Name; dbEntry.Number = phone.Number; dbEntry.LocationId = phone.LocationId; } } context.SaveChanges(); SaveLocPhonesXMLFile(); }
// GET: LocPhoneNums/Edit/5 public ActionResult Edit(int?id) { if (id == null) { TempData["alert"] = "Sorry, I could not find the item you were looking for. Please try again."; return(View("~/Locations")); } LocPhoneNums locPhoneNums = context.LocPhoneNums.FirstOrDefault(p => p.Id == id); LocationPhonesViewModel model = new LocationPhonesViewModel { Location = context.Locations.FirstOrDefault(l => l.Id == locPhoneNums.LocationId), LocPhoneNums = locPhoneNums }; if (locPhoneNums == null) { return(HttpNotFound()); } return(View(model)); }