public ActionResult Save(CostCenterFormViewModel costcenterViewModel) { if (!ModelState.IsValid) { return(View("CostCenterForm", costcenterViewModel)); } //else we continue with new or update if (costcenterViewModel.CostCenter.Id == 0)//then we know it's a new cost center { var costcenter = costcenterViewModel.CostCenter; _context.CostCenters.Add(costcenter); _context.SaveChanges(); } else { var costcenterInDb = _context.CostCenters.Find(costcenterViewModel.CostCenter.Id); if (costcenterInDb == null) { return(HttpNotFound()); } costcenterInDb.Code = costcenterViewModel.CostCenter.Code; costcenterInDb.Name = costcenterViewModel.CostCenter.Name; _context.SaveChanges(); } return(RedirectToAction("Index")); }
//Create new cost center public ActionResult New() { var costcenter = new CostCenter { Id = 0 }; var costcenterViewModel = new CostCenterFormViewModel { ActionIndicator = 1, CostCenter = costcenter }; return(View("CostCenterForm", costcenterViewModel)); }
//edit existing cost center public ActionResult Edit(int id) { var costcenterInDb = _context.CostCenters.Find(id); if (costcenterInDb == null) { return(HttpNotFound()); } var costcenterViewModel = new CostCenterFormViewModel { ActionIndicator = 2, CostCenter = costcenterInDb }; return(View("CostCenterForm", costcenterViewModel)); }