public PicturesViewModel() { ProjectDetail = new ProjectDetailsViewModel(); Education = new EducationViewModel(); Experience = new ExperiencesViewModel(); Skill = new SkillsViewModel();/**/ }
// GET: Skills/Create public ActionResult Create() { var skill = new SkillsViewModel(); skill.Experiences = db.Experiences.ToList(); skill.Projects = db.Projects.ToList(); return View(skill); }
private void AddOrUpdateProjects(SkillsViewModel skill, string[] selectedProjectsID) { foreach (var selectedProjectId in selectedProjectsID) { var projectFind = db.Projects.Find(Int32.Parse(selectedProjectId)); //db.Projects.Attach(projectFind); skill.Projects.Add(projectFind); } }
public ActionResult Create(SkillsViewModel skillsViewModel, string[] selectedProjectsID, string[] selectedExperiencesID) { if (ModelState.IsValid) { var skill = skillsViewModel; skill.Projects = new List<ProjectsViewModel>(); skill.Experiences = new List<ExperiencesViewModel>(); AddOrUpdateProjects(skill, selectedProjectsID); AddOrUpdateExperiences(skill, selectedExperiencesID); db.Skills.Add(skill); db.SaveChanges(); return RedirectToAction("Index"); } return View(skillsViewModel); }
private void AddOrUpdateExperiences(SkillsViewModel skill, string[] selectedExperiencesID) { foreach (var selectedExperienceId in selectedExperiencesID) { var experienceFind = db.Experiences.Find(Int32.Parse(selectedExperienceId)); //db.Experiences.Attach(experienceFind); skill.Experiences.Add(experienceFind); } }
// GET: Skills/Create public ActionResult Create() { var skillViewModel = new SkillsViewModel { Projects = db.Projects.ToList(), Experiences = db.Experiences.ToList() };// { Projects = PopulateProjectData(), Experiences = PopulateExperienceData() }; return View(skillViewModel); }
private void addOrUpdateSkillWithObjects(SkillsViewModel skill , IEnumerable<string> listExperiencesOfSkillId, IEnumerable<string> listProjectsIdOfSkill , IEnumerable<string> isExperienceSelected, IEnumerable<string> isProjectSelected) { // Experience handling List<int> listAddedExperiencesId = new List<int>(); if ( isExperienceSelected != null ) { int id; ExperiencesViewModel newExperience; for (int i = 0; i< isExperienceSelected.Count(); i++) { string isSelected = isExperienceSelected.ElementAt(i).Split('-').ToList()[0]; string isSelectedId = isExperienceSelected.ElementAt(i).Split('-').ToList()[1]; id = Int32.Parse(isSelectedId); newExperience = db.Experiences.Find(id); if ( isSelected == "true" ) { skill.Experiences.Add(newExperience); listAddedExperiencesId.Add(id); } } } // removing uncheck Experiences if ( listExperiencesOfSkillId != null ) { foreach (var experienceIdString in listExperiencesOfSkillId) { var experienceId = Int32.Parse(experienceIdString); if ((listAddedExperiencesId != null && !listAddedExperiencesId.Contains(experienceId)) || listAddedExperiencesId == null) { var experienceToRemove = db.Experiences.Find(experienceId); skill.Experiences.Remove(experienceToRemove); } } } // Project handling List<int> listAddedProjectsId = new List<int>(); if ( isProjectSelected != null ) { int id; ProjectsViewModel newProject; for (int i = 0; i < isProjectSelected.Count(); i++) { string isSelected = isProjectSelected.ElementAt(i).Split('-').ToList()[0]; string isSelectedId = isProjectSelected.ElementAt(i).Split('-').ToList()[1]; id = Int32.Parse(isSelectedId); newProject = db.Projects.Where(y => y.ID == id).Include("ProjectDetail").DefaultIfEmpty().Single(); if ( isSelected == "true" ) { skill.Projects.Add(newProject); listAddedProjectsId.Add(id); } } } // removing uncheck Projects if (listProjectsIdOfSkill != null) { foreach (var projectIdString in listProjectsIdOfSkill) { var projectId = Int32.Parse(projectIdString); if ( (listAddedProjectsId != null && !listAddedProjectsId.Contains(projectId)) || listAddedProjectsId == null ) { var projectToRemove = db.Projects.Where(x => x.ID == projectId).Include("ProjectDetail").First(); skill.Projects.Remove(projectToRemove); } } } }