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 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 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(); }