/// <summary> /// Setup view model after post, where user select fruit data (For edit view - httppost) /// </summary> /// <param name="postedRaces"></param> /// <returns></returns> private UltimateViewModel GetRacesModel(UltimateViewModel ultimate, PostedRaces postedRaces) { //Setup properties var rvm = ultimate.RacesViewModel; var selectedRaces = rvm.SelectedRaces; var postedRaceIds = new string[0]; postedRaces = rvm.PostedRaces; if (postedRaces == null) { postedRaces = new PostedRaces(); } //Save selected ids (if array of posted exists and not empty) if (postedRaces.RaceIDs != null && postedRaces.RaceIDs.Any()) { postedRaceIds = postedRaces.RaceIDs; } //Create list of fruits (if any selected ids saved) //if (postedRaceIds.Any()) //{ // //selectedRaces = RaceRepository.GetAll().Where(x => postedRaceIds.Any(s => x.Id.ToString().Equals(s))).ToList(); // selectedRaces = db.Races.Where(x => postedRaceIds.Any(s => x.Id.ToString().Equals(s))).ToList(); //} //Setup view model rvm.AvailableRaces = db.Races.ToList();//RaceRepository.GetAll().ToList(); rvm.SelectedRaces = selectedRaces; rvm.PostedRaces = postedRaces; ultimate.RacesViewModel = rvm; return(ultimate); }
public ActionResult Edit(/*[Bind(Include = "Id,StudentNumber,FirstName,MiddleName,LastName,SchoolEmail,OtherEmail,Phone,GendersId,RaceOther,DegreeProgramsId,TracksId,DegreeStart,DegreeEnd")] Student student*/ UltimateViewModel ultimate, PostedRaces postedRaces) { try { if (ModelState.IsValid) { db.Entry(ultimate.Student).State = EntityState.Modified; //Initiate postedRaces; postedRaces = ultimate.RacesViewModel.PostedRaces; //Create current/previously-checked race list var currPRList = db.PersonRaces.Where(s => s.StudentID == ultimate.Student.Id && s.IsSelectedPR.Equals(true)).ToList(); var currRaceList = new List <Races>(); foreach (var item in currPRList) { currRaceList.Add(item.Race); } //Create new checked race list var newList = new List <Races>(); foreach (var item in postedRaces.RaceIDs) { int raceId = Int32.Parse(item); var race = db.Races.Where(s => s.Id == raceId).ToList().Single(); newList.Add(race); } //Iterate through new items; if there are no new items in current list, add them foreach (var nItem in newList) { if (!currRaceList.Contains(nItem)) { var personRace = new PersonRaces(); int raceId = nItem.Id; var race = db.Races.Where(s => s.Id == raceId).ToList().Single(); personRace.Student = ultimate.Student; personRace.Race = race; personRace.IsSelectedPR = true; db.PersonRaces.Add(personRace); db.SaveChanges(); } } //Iterate through the current items; if there are no item in new list, delete them foreach (var cItem in currRaceList) { if (!newList.Contains(cItem)) { var race = db.Races.Where(s => s.Id == cItem.Id).ToList().Single(); var prEntity = db.PersonRaces.SingleOrDefault(s => s.StudentID == ultimate.Student.Id && s.RaceID == cItem.Id && s.IsSelectedPR.Equals(true)); prEntity.IsSelectedPR = false; db.SaveChanges(); } } ultimate.RacesViewModel.SelectedRaces = newList; db.SaveChanges(); return(RedirectToAction("Edit", "Student", new { id = ultimate.Student.Id })); } } catch (DataException /*dex*/) { //Log the error (uncomment dex cariable name and ad a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, please see your system administrator."); } //View Bags for Dropdowns ViewBag.GendersIdBag = new SelectList(db.CommonFields.Where(o => o.Category == "Gender"), "Id", "Name"); ViewBag.DegreeProgramsIdBag = new SelectList(db.CommonFields.Where(o => o.Category == "DegreeProgram"), "Id", "Name"); ViewBag.DegreeProgramsIdBag = new SelectList(db.CommonFields.Where(o => o.Category == "DegreeProgram"), "Id", "Name"); ViewBag.TracksIdBag = new SelectList(db.CommonFields.Where(o => o.Category == "Track"), "Id", "Name"); ViewBag.DegreeStartSemsIdBag = new SelectList(db.CommonFields.Where(o => o.Category == "Season"), "Id", "Name"); ViewBag.CitizenshipStatsIdBag = new SelectList(db.CommonFields.Where(o => o.Category == "CitizenshipStatus"), "Id", "Name"); ViewBag.MsctrFacultyIdBag = new SelectList(db.CommonFields.Where(o => o.Category == "MsctrFaculty").OrderBy(o => o.DisplayOrder), "Id", "Name"); ViewBag.MsctrFacultyIdBag2 = new SelectList(db.CommonFields.Where(o => o.Category == "MsctrFaculty" && o.Name == "Other").OrderBy(o => o.DisplayOrder), "Id", "Name"); return(View(GetRacesModel(ultimate, postedRaces))); }