コード例 #1
0
        public ActionResult Create(/*[Bind(Include = "StudentNumber,FirstName,MiddleName,LastName,SchoolEmail,OtherEmail,Phone,GendersId,RaceOther,DegreeProgramsId,TracksId,DegreeStart,DegreeEnd")] Student student,*/ UltimateViewModel ultimate)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //Add recorded student
                    db.Students.Add(ultimate.Student);

                    ultimate.RacesViewModel.AvailableRaces = db.Races.ToList();

                    //For current student, add to personRaceTable, student id number and postedRace number
                    foreach (var item in ultimate.RacesViewModel.PostedRaces.RaceIDs)
                    {
                        var personRace = new PersonRaces();
                        int raceId     = Int32.Parse(item);
                        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();
                    }
                    db.SaveChanges();
                    return(RedirectToAction("Edit", "Student", new { id = ultimate.Student.Id }));
                    //return RedirectToAction("Index");
                }
            }
            catch (DataException /*dex*/)
            {
                //Log the error (uncomment dex cariable name and add 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.TracksIdBag           = new SelectList(db.CommonFields.Where(o => o.Category == "Track"), "Id", "Name");
            ViewBag.PlansIdBag            = new SelectList(db.CommonFields.Where(o => o.Category == "Plan"), "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.EmploymentStatsIdBag  = new SelectList(db.CommonFields.Where(o => o.Category == "EmploymentStatus"), "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)));
        }
コード例 #2
0
        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)));
        }