public IActionResult Delete(Guid id) { StatusCodeViewModel sCodeVm = new StatusCodeViewModel(); using (StatusCodeDBContext db = new StatusCodeDBContext()) { using (var dbB = new BridgeDBContext()) { BridgeViewModel bridgeVm = new BridgeViewModel(); bridgeVm.BridgeList = dbB.Bridges.ToList(); bridgeVm.NewBridge = dbB.Bridges.Where( sc => sc.StatusId == id).FirstOrDefault(); if (bridgeVm.NewBridge == null) { sCodeVm.NewStatusCode = new StatusCode(); //find id sCodeVm.NewStatusCode.StatusCodeId = Guid.Parse(RouteData.Values["id"].ToString()); db.Entry(sCodeVm.NewStatusCode).State = EntityState.Deleted; db.SaveChanges(); TempData["ResultMessage"] = "Status Code deleted"; } else { TempData["ResultMessage"] = "This Status Code has dependencies, cannot delete!"; } } } return(RedirectToAction("Index")); }
public IActionResult Edit(StatusCodeViewModel obj) { //check for valid model if (ModelState.IsValid) { using (StatusCodeDBContext db = new StatusCodeDBContext()) { //object for view model StatusCode sc = obj.NewStatusCode; //retrieve primary key/id from route data sc.StatusCodeId = Guid.Parse(RouteData.Values["id"].ToString()); //update record status db.Entry(sc).State = EntityState.Modified; db.SaveChanges(); } } return(RedirectToAction("Index")); }