//edit developer public ActionResult Edit(int DeveloperId)//to find the developer { //find the developer Developer developer = context.Find(DeveloperId); if (developer == null) { return(HttpNotFound()); } else { DeveloperManagerViewModel ViewModel = new DeveloperManagerViewModel(); ViewModel.Developer = developer; ViewModel.DeveloperId = DeveloperId; ViewModel.DeveloperPositions = developerPositions.Collection(); //Get developers potential supervisors from the database based on his role using (var dbContext = new DataContext()) { //3 Cases if (developer.Position == "Developer") { var potentialSuperiorsList = dbContext.Developers .SqlQuery("SELECT * FROM [StaffPositions].[dbo].[Developers] where Position = 'Team Lead' ORDER BY FullName ASC") .ToList <Developer>(); ViewModel.PotentialSuperiors = potentialSuperiorsList; } else if (developer.Position == "Team Lead") { var potentialSuperiorsList = dbContext.Developers .SqlQuery("SELECT * FROM [StaffPositions].[dbo].[Developers] where Position = 'Manager' ORDER BY FullName ASC") .ToList <Developer>(); ViewModel.PotentialSuperiors = potentialSuperiorsList; } else //Manager has no superior { List <Developer> potentialSuperiorsList = new List <Developer>(); potentialSuperiorsList.Add(new Developer { FirstName = "aaa", LastName = "aaa", FullName = "Not Supervised", Email = "*****@*****.**", Position = "aaa", Photo = "aaa", DeveloperId = 1000, SuperiorID = 10000, SuperiorName = "Not Supervised" }); ViewModel.PotentialSuperiors = potentialSuperiorsList; } } return(View(ViewModel)); } }
[HttpPost] //getting info from a page public ActionResult Edit(DeveloperManagerViewModel DeveloperViewModel, HttpPostedFileBase file) //to edit the developer { int DeveloperId; Developer developer = DeveloperViewModel.Developer; DeveloperId = developer.DeveloperId; Developer developerToEdit = context.Find(DeveloperId); if (developerToEdit == null) { return(HttpNotFound()); } else { if (!ModelState.IsValid) { //return View(developer);//stay on the current page //do nothing } //update developer to edit// developerToEdit.FirstName = developer.FirstName; developerToEdit.LastName = developer.LastName; developerToEdit.FullName = developer.FirstName + " " + developer.LastName; developerToEdit.Photo = developer.Photo; developerToEdit.Position = DeveloperViewModel.Position; developerToEdit.SuperiorName = DeveloperViewModel.SuperiorName; //from postedfile if (file != null) { developerToEdit.Photo = developerToEdit.FullName + Path.GetExtension(file.FileName); //remane to always have a unique file reference file.SaveAs(Server.MapPath("//Content//DeveloperProfiles//") + developerToEdit.Photo); //save the product image into the ProductImages folder } //get superiorID from SQL in the future // get supervisor's name from the view //read SQL to find supervisor developerId and //if supervisor is manager, then set devtoedit managerid = supervisor dev Id and teamlead id to null //elseif supervisor is teamlead, then set devtoedit teamleadid = supervisor dev Id and manager id to null context.Commit(); //refresh memory return(RedirectToAction("Index")); //redirect to Index page, to view the updated list } }