private void UpdateSkill(string[] selectedSkill, int id, Posting postingToUpdate) { if (selectedSkill == null) { postingToUpdate.PostingSkills = new List <PostingSkill>(); return; } var selectedSkillHS = new HashSet <string>(selectedSkill); var postingSkills = new HashSet <int>(db.PostingSkills.Where(s => s.PostingID == id).Select(s => s.SkillID)); foreach (var s in db.Skills) { var SkillToUpdate = db.Skills.Find(s.ID); PostingSkill postingSkill = new PostingSkill { Posting = postingToUpdate, PostingID = id, Skill = SkillToUpdate, SkillID = s.ID }; if (selectedSkillHS.Contains(s.ID.ToString())) { if (!postingSkills.Contains(s.ID)) { db.PostingSkills.Add(postingSkill); } } else { if (postingSkills.Contains(s.ID)) { var selectedItem = (from i in db.PostingSkills where i.PostingID == id & i.SkillID == s.ID select i).Single(); db.PostingSkills.Remove(selectedItem); } } } }
public ActionResult Create([Bind(Include = "ID,pstNumPosition,pstFTE,pstSalary,pstCompensationType,pstJobDescription,pstOpenDate,pstEndDate,pstJobStartDate,pstJobEndDate,Enabled,PositionID" /*,CreatedBy,CreatedOn,UpdatedBy,UpdatedOn,RowVersion"*/)] Posting posting, string[] selectedQualification, string[] selectedDay, string[] selectedLocation, string[] selectedSkill, bool?SavedAsTemplate, string templateName) { try { string Locations = ""; string Requirements = ""; string Days = ""; string Skills = ""; if (SavedAsTemplate == null) { SavedAsTemplate = false; } if (selectedQualification != null) { foreach (var r in selectedQualification) { Requirements += r + ","; var qualificateToAdd = db.Qualification.Find(int.Parse(r)); JobRequirement jobRequirement = new JobRequirement { Posting = posting, Qualification = qualificateToAdd, PostingID = posting.ID, QualificationID = qualificateToAdd.ID }; db.JobRequirements.Add(jobRequirement); } } if (selectedLocation != null) { foreach (var l in selectedLocation) { Locations += l + ","; var locationToAdd = db.Locations.Find(int.Parse(l)); JobLocation jobLocation = new JobLocation { Posting = posting, Location = locationToAdd, PostingID = posting.ID, LocationID = locationToAdd.ID }; db.JobLocations.Add(jobLocation); } } if (selectedSkill != null) { foreach (var s in selectedSkill) { Skills += s + ","; var skillToAdd = db.Skills.Find(int.Parse(s)); PostingSkill postingSkill = new PostingSkill { Posting = posting, Skill = skillToAdd, PostingID = posting.ID, SkillID = skillToAdd.ID }; db.PostingSkills.Add(postingSkill); } } if (selectedDay != null) { posting.Days = new List <Day>(); foreach (var d in selectedDay) { Days += d + ","; var dayToAdd = db.Days.Find(int.Parse(d)); posting.Days.Add(dayToAdd); } } if (ModelState.IsValid) { if ((bool)SavedAsTemplate) { SavedAsTemplate_fn(templateName, posting, Requirements, Skills, Locations, Days); } db.Postings.Add(posting); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (RetryLimitExceededException) { logger.Error("Create/ Retry Limit Exceeded For Update"); ModelState.AddModelError("", "Unable to save changes after multiple attemps. Try Again!"); } catch (DataException dex) { logger.Error("Create/ Data Exception Error '{0}'", dex.ToString()); if (dex.InnerException.InnerException.Message.Contains("IX_Unique_Code")) { ModelState.AddModelError("PositionCode", "Unable to save changes. The Position Code is already existed."); } if (dex.InnerException.InnerException.Message.Contains("IX_Unique_Name")) { ModelState.AddModelError("", "Unable to save changes. The Template Name is already existed."); } else if (dex.InnerException.InnerException.Message.Contains("IX_Unique_Desc")) { ModelState.AddModelError("PositionDescription", "Unable to save changes. The Position can not have the same name."); } else { ModelState.AddModelError("", "Unable to save changes. Try Again!"); } } if (templateName != null) { posting = Template_fn(templateName, posting); return(View(posting)); } PopulateListBox(); PopulateDropdownList(posting); PopulateAssignedDay(posting); return(View(posting)); }