public ActionResult EditOrCreateSkill(string code, string adLoginID) { try { /* // APPROACH 1 => Allow EF to work out what has changed by comparing to copy in DB // Load current account from DB IntegrationWebSiteMvc.Models.Employee storedInstance = _modelContext.Employees.Single(p => p.ADLoginID == newInstance.ADLoginID); // Update the properties _modelContext.Entry(storedInstance).CurrentValues.SetValues(newInstance); // Save the changes _modelContext.SaveChanges(); */ /* // APPROACH 2 => Assumes changes even though may not be any _modelContext.Entry(newInstance).State = EntityState.Modified; _modelContext.SaveChanges(); */ BusinessObjects.WorkManagement.Skill newInstance = new BusinessObjects.WorkManagement.Skill(); newInstance.Code = code; IntegrationWebSiteMvc.Models.Employee storedInstance = _modelContext.Employees.Single(p => p.ADLoginID == adLoginID); if (storedInstance != null && storedInstance.Skills != null && storedInstance.Skills.ToList().Find(p => p.Code == newInstance.Code) != null) { _modelContext.Entry(storedInstance).CurrentValues.SetValues(newInstance); } else { // _modelContext.Employees.Single(p => p.ADLoginID == adLoginID).Skills.Add(newInstance); if (storedInstance.Skills == null) { storedInstance.Skills = new List<BusinessObjects.WorkManagement.Skill>(); } newInstance.ID = storedInstance.Skills.Count + 1; storedInstance.Skills.Add(newInstance); } _modelContext.SaveChanges(); return RedirectToAction("Index"); } catch(Exception excE) { throw excE; } }
public ActionResult EditOrCreateSkill(string code) { BusinessObjects.WorkManagement.Skill skill = null; if (!string.IsNullOrEmpty(code)) { BusinessObjects.WorkManagement.SkillCollection skills = _modelContext.GetSkills(); if (skills != null) { int index = skills.Find("Code", code); if (index < 0) { throw new Exception(string.Format("Unable to find Skill with Code {0}", code)); } skill = skills[index]; } } else { skill = new BusinessObjects.WorkManagement.Skill(); } return View(skill); }
public ActionResult EditOrCreateSkill(int id, string adLoginID) { BusinessObjects.WorkManagement.Skill newInstance = null; if (id > 0) { newInstance = _modelContext.Employees.Find(adLoginID).Skills.ToList().Find(p => p.ID == id); } else { newInstance = new BusinessObjects.WorkManagement.Skill(); } ViewBag.ADLoginID = adLoginID; ViewBag.Skills = new Models.WMSMetadata().GetSkills(); return View(newInstance); }