public ActionResult Save(TabletRepairViewModel tabletRepair) { Repair repair = tabletRepair; //implicit conversion with the help of implicit operator in TabletRepairviewModels class try { if (ModelState.IsValid) { //the line below is from: https://www.thereformedprogrammer.net/updating-a-many-to-many-relationship-in-entity-framework/ //also need to work to update this code to work with EDIT action. See article above. //this only works with EF configured many-to-many relationship. It will not work with custom join table with payload. repair.ProblemAreas = db.ProblemAreas.Where(p => tabletRepair.AssignedProblems.Contains(p.ID)).ToList(); db.Repairs.Add(repair); db.SaveChanges(); //lines below populat ordered parts. if (tabletRepair.OrderedPartIDs != null) { List <PartOrder> partOrders = new List <PartOrder>(); foreach (var part in tabletRepair.OrderedPartIDs) { partOrders.Add(new PartOrder() { OrderedOn = DateTime.Now, PartID = part, RepairID = repair.ID, IsPartReceived = false }); } db.PartOrders.AddRange(partOrders); db.SaveChanges(); } return(RedirectToAction("Details", "Tablets", new { id = repair.TabletID })); } } catch (DataException dex) { if (dex.InnerException.InnerException.Message.Contains("IX_VendorCaseNo")) { ModelState.AddModelError("VendorCaseNo", "Unable to save changes. Vendor Case No. must be unique"); } else { ModelState.AddModelError(string.Empty, $"Database Error occured Copy the error message and send it to Dima</br>: {dex.Message}. / {dex.InnerException.Message} / {dex.InnerException.InnerException.Message} "); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, $"Error occured Copy the error message and send it to Dima</br>: {ex.Message}. + {ex.InnerException.Message} + {ex.InnerException.InnerException.Message}"); } return(View(tabletRepair)); }
public ActionResult Save(int?tabletID) { if (!tabletID.HasValue) { //redirect to the tablet list if this page was accessed by accident without providing a valid tablet ID return(RedirectToAction("Index", "Tablets")); } var tabletRepair = new TabletRepairViewModel(tabletID.Value); return(View(tabletRepair)); }
public ActionResult Edit(TabletRepairViewModel tabletRepair) { Repair repair = tabletRepair; try { if (ModelState.IsValid) { // var problemsToAdd = db.ProblemAreas.Where(p => tabletRepair.AssignedProblems.Contains(p.ID)).ToList(); repair.ProblemAreas = new HashSet <ProblemArea>(); db.Entry(repair).State = EntityState.Modified; db.Entry(repair).Collection(p => p.ProblemAreas).Load(); db.Entry(repair).Collection(p => p.PartOrders).Load(); foreach (var partOrder in repair.PartOrders) { partOrder.IsPartReceived = tabletRepair.PartOrders.Where(p => p.ID == partOrder.ID).Select(p => p.IsPartReceived).SingleOrDefault(); partOrder.ReceivedOn = tabletRepair.PartOrders.Where(p => p.ID == partOrder.ID).Select(p => p.ReceivedOn).SingleOrDefault(); } repair.ProblemAreas = db.ProblemAreas.Where(p => tabletRepair.AssignedProblems.Contains(p.ID)).ToList(); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DbUpdateConcurrencyException) { //This is implemented by a virtue of having "RowVersion" field in our model and database. watch this video for details: https://youtu.be/Gi_kEbc5faQ ModelState.AddModelError(string.Empty, $"The record you were trying to update was modified by another user. Please go back and try again."); } catch (DataException dex) { if (dex.InnerException.InnerException.Message.Contains("IX_VendorCaseNo")) { ModelState.AddModelError("VendorCaseNo", "Unable to save changes. Vendor Case No. must be unique"); } else { ModelState.AddModelError(string.Empty, $"Database Error occured Copy the error message and send it to Dima </br>: {dex.Message}. + {dex.InnerException.Message} + {dex.InnerException.InnerException.Message}"); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, $"Unexpected error occured. Copy the error message and send it to Dima {ex.Message} | {ex.InnerException.InnerException.Message}" + $"{ex.InnerException.InnerException.Message}"); } return(View(tabletRepair)); }
// GET: Repairs/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Repair repair = db.Repairs.Include(r => r.PartOrders).Include(r => r.ProblemAreas).Include(r => r.Tablet).FirstOrDefault(r => r.ID == id); if (repair == null) { return(HttpNotFound()); } TabletRepairViewModel tabletRepair = repair; return(View(tabletRepair)); }
public ActionResult Save(TabletRepairViewModel tabletRepair) { if (ModelState.IsValid) { Repair repair = tabletRepair; //implicit conversion with the help of implicit operator in TabletRepairviewModels class repair.UpdatedOn = DateTime.Now; repair.CreatedOn = DateTime.Now; repair.IsClosed = tabletRepair.IsClosed; //repair.TechID = 2; //this is temporary until Auth and Oauth is implemented; db.Repairs.Add(repair); db.SaveChanges(); return(RedirectToAction("Details", "Tablets", new { id = repair.TabletID })); } return(View(tabletRepair)); }
public ActionResult Save(int?tabletID) { if (!tabletID.HasValue) { return(RedirectToAction("Index", "Tablets")); } var tablet = db.Tablets.Find(tabletID); var tech = db.Teches.Find(2); //magic number but will be replaces with Tech's info later; var tabletRepair = new TabletRepairViewModel { //TabletID = tablet.ID, //TabletName = tablet.TabletName, //TechID = 2, //Kevin //TechName = tech.FullName }; return(View(tabletRepair)); }