public ActionResult UpdateDelete(ProfitCenterViewModel PRCViewModel, string command) { string PageAction = ""; bool result = false; user = (UserSession)Session["User"]; if (command == "Save") { ProfitCenterManager PRCManager = new ProfitCenterManager(); result = PRCManager.UpdateProfitCenter(PRCViewModel); PageAction = "UPDATE"; } else if (command == "Delete") { ProfitCenterManager PRCManager = new ProfitCenterManager(); result = PRCManager.DeleteProfitCenter(PRCViewModel); PageAction = "DELETE"; } if (result) { TempData["SuccessMessage"] = PageAction + " successful"; new AuditLogManager().Audit(user.Username, DateTime.Now, "ProfitCenter", PageAction, PRCViewModel.Id, PRCViewModel.PRFCNT); } else { TempData["ErrorMessage"] = PageAction + " failed"; } return(RedirectToAction("Index")); }
// GET: ProfitCenter public ActionResult Index() { // Validate log in and user access UserAccessSession UASession = (UserAccessSession)Session["UserAccess"]; // LOC -> ProfitCenter // Refer to UserAccessSession if (UASession == null || !UASession.PRC) { return(RedirectToAction("Login", "Account")); } user = (UserSession)Session["User"]; Session["CurrentPage"] = new CurrentPageSession("PRC", "HOME", "LOG"); // Get all data stored in DB table ProfitCenterManager PRCManager = new ProfitCenterManager(); ProfitCenterViewModel PRCViewModel = new ProfitCenterViewModel(); PRCViewModel.PCList = PRCManager.GetPRC(); if (PRCViewModel.PCList == null || PRCViewModel.PCList.Count() == 0) { PRCViewModel.PCList = new List <ProfitCenterViewModel>(); } // return View with ViewModel return(View(PRCViewModel)); }
public List <ProfitCenterViewModel> GetPRC() { using (CFMMCDEntities db = new CFMMCDEntities()) { List <ProfitCenterViewModel> PRCList = new List <ProfitCenterViewModel>(); foreach (PROFIT_CEN prc in db.PROFIT_CEN) { ProfitCenterViewModel PRCViewModel = new ProfitCenterViewModel(); PRCViewModel.Id = (prc.Id).ToString(); PRCViewModel.PRFCNT = prc.PRFCNT; // Add to List PRCList.Add(PRCViewModel); } return(PRCList); } }
public bool UpdateProfitCenter(ProfitCenterViewModel PRCViewModel) { using (CFMMCDEntities db = new CFMMCDEntities()) { PROFIT_CEN prcRow = new PROFIT_CEN(); prcRow.Id = int.Parse(PRCViewModel.Id); prcRow.PRFCNT = PRCViewModel.PRFCNT; try { if (db.PROFIT_CEN.Where(o => o.Id.ToString().Equals(PRCViewModel.Id)).Any()) { var rowToRemove = db.PROFIT_CEN.Single(o => o.Id.ToString().Equals(PRCViewModel.Id)); db.PROFIT_CEN.Remove(rowToRemove); db.PROFIT_CEN.Add(prcRow); } else { db.PROFIT_CEN.Add(prcRow); } db.SaveChanges(); return(true); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Source); System.Diagnostics.Debug.WriteLine(e.Message); System.Diagnostics.Debug.WriteLine(e.StackTrace); System.Diagnostics.Debug.WriteLine(e.InnerException); Exception f = e.InnerException; while (f != null) { System.Diagnostics.Debug.WriteLine("INNER:"); System.Diagnostics.Debug.WriteLine(f.Message); System.Diagnostics.Debug.WriteLine(f.Source); f = f.InnerException; } System.Diagnostics.Debug.WriteLine(e.Data); return(false); } } }