public ActionResult EditCaller(Caller callerModel) { if (ModelState.IsValid) { tblCaller caller = new tblCaller() { Active = callerModel.Active, Assignment = callerModel.Assignment, CallerEmail = callerModel.CallerEmail, MobilePhone = callerModel.MobilePhone, CallerFirstName = callerModel.CallerFirstName, CallerLastName = callerModel.CallerLastName, CallerPager = callerModel.CallerPager, OfficePhone = callerModel.OfficePhone, CallerTitle = callerModel.CallerTitle, ID = callerModel.CallerId }; try { using (var context = new lifeflightapps()) { context.tblCallers.AddOrUpdate(caller); //db.tblCallers.Attach(caller); //var entry = db.Entry(caller); //entry.State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); var preferredNumber = context.tblCallerPreferredNumbers.Where(x => x.CallerId == callerModel.CallerId).FirstOrDefault(); string number = GetPreferredNumber(callerModel, callerModel.PreferredNumberType); if (preferredNumber != null) { preferredNumber.PreferredNumber = number; preferredNumber.PreferredType = callerModel.PreferredNumberType; context.SaveChanges(); } else { tblCallerPreferredNumber cpn = new tblCallerPreferredNumber() { CallerId = callerModel.CallerId, PreferredNumber = number, PreferredType = callerModel.PreferredNumberType }; context.tblCallerPreferredNumbers.Add(cpn); context.SaveChanges(); } return(RedirectToAction("Index")); } } catch (DataException dx) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); throw dx; } } return(View(callerModel)); }
public ActionResult Create(Caller CallerModel) { int CallerId = 0; var preferred = CallerModel.PreferredNumberType; string preferredNumber = string.Empty; try { tblCaller tcaller = new tblCaller() { Active = true, Assignment = CallerModel.Assignment, CallerEmail = CallerModel.CallerEmail, OfficePhone = CallerModel.OfficePhone, CallerFirstName = CallerModel.CallerFirstName, CallerLastName = CallerModel.CallerLastName, CallerPager = CallerModel.CallerPager, MobilePhone = CallerModel.MobilePhone, CallerTitle = CallerModel.CallerTitle }; preferredNumber = GetPreferredNumber(CallerModel, preferred); tblCallerPreferredNumber callerPreferredNumber = new tblCallerPreferredNumber() { PreferredNumber = preferredNumber, PreferredType = CallerModel.PreferredNumberType }; using (var context = new lifeflightapps()) { try { context.tblCallers.Add(tcaller); context.SaveChanges(); CallerId = tcaller.ID; } catch (Exception ex) { throw ex; } callerPreferredNumber.CallerId = CallerId; context.tblCallerPreferredNumbers.Add(callerPreferredNumber); context.SaveChanges(); } return(RedirectToAction("Index")); } catch (Exception ex) { throw ex; } }
// GET: Callers/Edit/5 public ActionResult Edit(int id) { Caller model = new Caller(); tblCaller dbcaller = new tblCaller(); tblCallerPreferredNumber preferredNumber = new tblCallerPreferredNumber(); using (var context = new lifeflightapps()) { dbcaller = context.tblCallers.Find(id); preferredNumber = context.tblCallerPreferredNumbers.Where(x => x.CallerId == id).OrderByDescending(x => x.CallerPreferredID).FirstOrDefault(); } model = new Caller() { Active = dbcaller.Active.HasValue?dbcaller.Active.Value:false, CallerFirstName = dbcaller.CallerFirstName, CallerLastName = dbcaller.CallerLastName, CallerTitle = dbcaller.CallerTitle, Assignment = dbcaller.Assignment, OfficePhone = dbcaller.OfficePhone, CallerPager = dbcaller.CallerPager, MobilePhone = dbcaller.MobilePhone, CallerEmail = dbcaller.CallerEmail, CallerId = dbcaller.ID }; // if (preferredNumber != null) // { // model.PreferredNumberType = preferredNumber.PreferredType; // TempData["PreferredType"] = preferredNumber.PreferredType; // } // return View(model); //} model.PreferredNumberTypes = GetPreferredTypes(); if (preferredNumber != null) { foreach (var item in model.PreferredNumberTypes) { if (item.Value == preferredNumber.PreferredType) { item.Selected = true; break; } } } return(View(model)); }