public ActionResult RequestEdit(int?requestId) { using (MainDBEntities mainDB = new MainDBEntities()) { var requstResult = mainDB.RequestTables.FirstOrDefault(a => a.RequestID == requestId); var viewModel = new RequestEditViewModel(requstResult); viewModel.GetCompany(mainDB.ClientTables.FirstOrDefault(x => x.ClientID == requstResult.ClientID)); viewModel.GetType(mainDB.TypeTables.FirstOrDefault(x => x.TypeID == requstResult.TypeID)); viewModel.GetStatus(mainDB.StatusTables.FirstOrDefault(x => x.StatusID == requstResult.StatusID)); viewModel.GetUser(mainDB.UserAccounts.FirstOrDefault(x => x.UserID == requstResult.UserID)); var statuses = mainDB.StatusTables.ToArray(); viewModel.GetStatuses(statuses); var detailsList = requstResult.RequestDetails.Select(x => new DetailsViewModel { StageNumber = x.StageNumber, StageDate = x.StageDate.Value.ToShortDateString(), StageDesc = x.StageDesc, StageTime = x.StageTime, Status = statuses.FirstOrDefault(y => y.StatusID == x.StatusID).StatusName, UserName = mainDB.UserAccounts.FirstOrDefault(y => x.UserID == requstResult.UserID).UserName }).ToArray(); viewModel.Details = detailsList; return(View(viewModel)); } }
public ActionResult RequestEditPOST(RequestEditViewModel dupa) { ActionResult actionResult; var tmp = dupa; if (ModelState.IsValid) { using (MainDBEntities mainDB = new MainDBEntities()) { var requstResult = mainDB.RequestTables.FirstOrDefault(a => a.RequestID == dupa.RequestID); if (requstResult != null) { requstResult.Title = dupa.Title; requstResult.RequestDate = dupa.RequestDate; requstResult.StatusID = dupa.StatusId; requstResult.Description = dupa.Description; requstResult.RequestTime = dupa.RequestTime; requstResult.Solution = dupa.Solution; } mainDB.SaveChanges(); } actionResult = RedirectToAction("Index"); } else { using (MainDBEntities mainDB = new MainDBEntities()) { var statuses = mainDB.StatusTables.ToArray(); dupa.GetStatuses(statuses); var requstResult = mainDB.RequestTables.FirstOrDefault(a => a.RequestID == dupa.RequestID); var detailsList = requstResult.RequestDetails.Select(x => new DetailsViewModel { StageNumber = x.StageNumber, StageDate = x.StageDate.Value.ToShortDateString(), StageDesc = x.StageDesc, StageTime = x.StageTime, Status = statuses.FirstOrDefault(y => y.StatusID == x.StatusID).StatusName, UserName = mainDB.UserAccounts.FirstOrDefault(y => x.UserID == requstResult.UserID).UserName }).ToArray(); dupa.Details = detailsList; } actionResult = View("RequestEdit", dupa); } return(actionResult); }