public ActionResult Edit(LocHourCats locHourCats) { /***** Logging initial settings *****/ DbChangeLog log = new DbChangeLog(); log.UserName = User.Identity.Name; log.Controller = "LocHourCats"; log.Action = (locHourCats.Id != 0) ? "Edit" : "Create"; log.ItemId = locHourCats.Id; // if this is an edit to an exhisting item, record the old item to the log if (log.Action == "Edit") { LocHourCats oldCat = context.LocHourCats.FirstOrDefault(c => c.Id == locHourCats.Id); log.BeforeChange = Domain.Extensions.DbLogExtensions.LocCatToString(oldCat); } // record the newly attempted change log.AfterChange = Domain.Extensions.DbLogExtensions.LocCatToString(locHourCats); /***** end Logging initial settings *****/ if (ModelState.IsValid) { try { context.SaveLocHourCat(locHourCats); // 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 = locHourCats.Id; log.Success = true; TempData["message"] = string.Format("{0} has been saved", locHourCats.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", locHourCats.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 = locHourCats.Id }); }
public static string LocCatToString(LocHourCats cat) { string locString = "<table class='table table-striped'>"; locString += "<tr><th>ID:</th><td>" + cat.Id.ToString() + "</td></tr>"; locString += "<tr><th>Name:</th><td>" + cat.Name + "</td></tr>"; locString += "<tr><th>Location ID:</th><td>" + cat.LocationId + "</td></tr>"; locString += "</table>"; return locString; }
public void SaveLocHourCat(LocHourCats hourCat) { if (hourCat.Id == 0) { context.LocHourCats.Add(hourCat); } else { LocHourCats dbEntry = context.LocHourCats.Find(hourCat.Id); if (dbEntry != null) { dbEntry.Name = hourCat.Name; dbEntry.LocationId = hourCat.LocationId; } } context.SaveChanges(); SaveLocHoursXMLFile(); }