public ActionResult Edit(LocServices locServices) { /***** Logging initial settings *****/ DbChangeLog log = new DbChangeLog(); log.UserName = User.Identity.Name; log.Controller = "LocServices"; log.Action = (locServices.Id != 0) ? "Edit" : "Create"; log.ItemId = locServices.Id; // if this is an edit to an exhisting item, record the old item to the log if (log.Action == "Edit") { LocServices oldService = context.LocServices.FirstOrDefault(c => c.Id == locServices.Id); log.BeforeChange = Domain.Extensions.DbLogExtensions.LocServiceToString(oldService); } // record the newly attempted change log.AfterChange = Domain.Extensions.DbLogExtensions.LocServiceToString(locServices); /***** end Logging initial settings *****/ if (ModelState.IsValid) { try { context.SaveLocService(locServices); // 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 = locServices.Id; log.Success = true; TempData["message"] = string.Format("{0} has been saved", locServices.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", locServices.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); LocationServicesViewModel model = new LocationServicesViewModel { Location = context.Locations .FirstOrDefault(p => p.Id == locServices.LocationId), LocServices = locServices }; return View(model); }
public static string LocServiceToString(LocServices serv) { string locString = "<table class='table table-striped'>"; locString += "<tr><th>ID:</th><td>" + serv.Id.ToString() + "</td></tr>"; locString += "<tr><th>Name:</th><td>" + serv.Name + "</td></tr>"; locString += "<tr><th>Icon Class Name:</th><td>" + serv.IconClassName + "</td></tr>"; locString += "<tr><th>Featured?:</th><td>" + serv.Featured.ToString() + "</td></tr>"; locString += "<tr><th>Location ID:</th><td>" + serv.LocationId + "</td></tr>"; locString += "</table>"; return locString; }
public void SaveLocService(LocServices service) { if (service.Id == 0) { context.LocServices.Add(service); } else { LocServices dbEntry = context.LocServices.Find(service.Id); if (dbEntry != null) { dbEntry.Featured = service.Featured; dbEntry.Name = service.Name; dbEntry.Description = service.Description; dbEntry.Featured = service.Featured; dbEntry.IconClassName = service.IconClassName; dbEntry.LocationId = service.LocationId; } } context.SaveChanges(); SaveLocServicesXMLFile(); }