public ActionResult UpdateDetail(LedgerDetailModel model) { using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities()) { var theDetail = context.LedgerDetails.FirstOrDefault(d => d.LedgerDetailId == model.DetailId); if (theDetail == default(LedgerDetail)) { return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger Detail!", Message = "The ledger detail you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } }); } else if (theDetail.Ledger.UserProfile.UserName != User.Identity.Name && theDetail.Ledger.Editors.FirstOrDefault(u => u.UserName == User.Identity.Name) == default(UserProfile)) { return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are neither the owner nor an editor of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } }); } theDetail.When = model.When; theDetail.PaySource = model.PaySource; theDetail.Payor = model.Payor; theDetail.Amount = model.Amount; theDetail.Category = model.Category; theDetail.Memo = model.Memo; context.SaveChanges(); return RedirectToAction("Detail", new { id = theDetail.Ledger.LedgerId }); } }
public ActionResult Detail(int id) { using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities()) { var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == id); if (theLedger == default(Ledger)) { return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } }); } else if (theLedger.UserProfile.UserName != User.Identity.Name && theLedger.Editors.FirstOrDefault(u => u.UserName == User.Identity.Name) == default(UserProfile)) { return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are neither the owner nor an editor of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } }); } LedgerModel theModel = new LedgerModel(); theModel.Id = theLedger.LedgerId; theModel.Name = theLedger.LedgerName; theModel.Details = new List<LedgerDetailModel>(); theModel.Owner = theLedger.UserProfile.UserName; theModel.Editors = theLedger.Editors.Select(u => u.UserName).ToList(); theLedger.LedgerDetails.OrderByDescending(i=>i.When.ToString("yyyyMMdd")+i.LedgerDetailId.ToString("X8")).ToList().ForEach(ld => { LedgerDetailModel theDetail = new LedgerDetailModel(); theDetail.DetailId = ld.LedgerDetailId; theDetail.When = ld.When; theDetail.PaySource = ld.PaySource; theDetail.Payor = ld.Payor; theDetail.Amount = ld.Amount; theDetail.Memo = ld.Memo; theDetail.Category = ld.Category; theModel.Details.Add(theDetail); }); if (theLedger.LedgerDetails.Any()) { theModel.LatestWhen = theLedger.LedgerDetails.Select(d => d.When).Max(); } else { theModel.LatestWhen = DateTime.Now; } return View(theModel); } }