public ActionResult Create(Project project, List <HttpPostedFileBase> Photos, string[] Technologies) { db.Projects.Add(project); db.SaveChanges(); List <TechnologyProject> TechnologyProjectss = new List <TechnologyProject>(); foreach (var tech in Technologies) { TechnologyProject technologyProject = new TechnologyProject(); technologyProject.ProjectId = project.Id; technologyProject.Technology = tech; TechnologyProjectss.Add(technologyProject); } db.TechnologyProjects.AddRange(TechnologyProjectss); foreach (var photos in Photos) { string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + photos.FileName.Replace(" ", "_"); string path = System.IO.Path.Combine(Server.MapPath("~/Upload"), filename); photos.SaveAs(path); Photo photo = new Photo(); photo.PhotoName = filename; photo.ProjectId = project.Id; db.Photos.Add(photo); } db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit(ProjectViewModel projectVM) { if (ModelState.IsValid) { if (projectVM == null) { return(RedirectToAction(nameof(Index))); } projectVM.Technologies = PopulateTechnologiesDropdown();//productVM.Products przybiera to co zwraca dropdown int?projectID = projectVM.ProjectID; //passing dropdown selected items to the collection List <SelectListItem> selectedItems = projectVM.Technologies.Where(p => projectVM.TechnologyIds.Contains(int.Parse(p.Value))).ToList(); Project project; //for new project if (projectID == 0) { project = new Project(); } //for edit project else { //repo get project project = repositoryProj.AllProjects.FirstOrDefault(o => o.ProjectID == projectID); } //repo save project repositoryProj.SaveProject(project, projectVM); //remove all technologies for this project to avoid duplicated keys repositoryTechProj.RemoveTechs(projectVM.ProjectID); //new tech list for project if (selectedItems != null) { //make new technology list for this project project.TechnologiesProjects = new List <TechnologyProject>(); foreach (var item in selectedItems) { var technologyAdd = new TechnologyProject { ProjectID = project.ProjectID, TechnologyID = int.Parse(item.Value) }; //repo new pair project - technology repositoryTechProj.NewPair(technologyAdd); } } return(RedirectToAction(nameof(Index))); } else { projectVM.Technologies = PopulateTechnologiesDropdown(); return(View(projectVM)); } }
public ActionResult Edit(Project project, List <HttpPostedFileBase> Photos, string[] Technologies) { Project TheProject = db.Projects.Where(u => u.Id == project.Id).FirstOrDefault(); if (TheProject != null) { TheProject.Name = project.Name; TheProject.Customer = project.Customer; TheProject.Date = project.Date; TheProject.Category = project.Category; TheProject.Description = project.Description; TheProject.Web = project.Web; db.SaveChanges(); } List <TechnologyProject> technologyProjects = new List <TechnologyProject>(); technologyProjects = db.TechnologyProjects.Where(u => u.ProjectId == TheProject.Id).ToList(); db.TechnologyProjects.RemoveRange(technologyProjects); if (Photos.FirstOrDefault()?.ContentLength > 0) { foreach (var photos in Photos) { string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + photos.FileName.Replace(" ", "_"); string path = System.IO.Path.Combine(Server.MapPath("~/Upload"), filename); photos.SaveAs(path); Photo photo = new Photo(); photo.PhotoName = filename; photo.ProjectId = project.Id; db.Photos.Add(photo); } } List <TechnologyProject> TechnologyProjectss = new List <TechnologyProject>(); foreach (var tech in Technologies) { TechnologyProject technologyProject = new TechnologyProject(); technologyProject.ProjectId = project.Id; technologyProject.Technology = tech; TechnologyProjectss.Add(technologyProject); } db.TechnologyProjects.AddRange(TechnologyProjectss); db.SaveChanges(); return(RedirectToAction("Index")); }