//ids : the unique id for the course record public ActionResult registerSelected(string[] ids, string studentRecordID, string stID) { string studentUID = ""; string sID = ""; if (String.IsNullOrEmpty(studentRecordID) && String.IsNullOrEmpty(stID)) { studentUID = Request.QueryString["uid"]; sID = Request.QueryString["studentID"]; ViewData["studentRecordID"] = studentUID; ViewData["stID"] = sID; } if (!(String.IsNullOrEmpty(studentRecordID) && String.IsNullOrEmpty(stID))) { studentUID = studentRecordID; sID = stID; ViewData["studentRecordID"] = studentUID; ViewData["stID"] = sID; } int studentKey = int.Parse(studentUID); //todo: add enrolments for selected classes (in ids array) // Use UiquerecordID not student id for the ""StudentID" category of enrollments //first get a list of the courses that needed to be added - done //then make an enrollment for each course for that student //this gets all the courses that were selected List<Course> coursesToAdd = new List<Course>(); if (ids != null && ids.Length != 0) { foreach (string s in ids) { int sint = int.Parse(s); //this section makes sure you cant create an identical enrollment twice var checker = from r in db.Enrollments select r; checker = checker.Where(e => e.CourseID == sint && e.Student.UniqueRecordID == studentKey); if (!(checker.Any())) { // no Match! coursesToAdd.Add(db.Courses.Find(sint)); } } } //gets the student in question Student studentToUSe = db.Students.Find(studentKey); //now make the enrollments and add them to the DB foreach (Course k in coursesToAdd) { Enrollment e = new Enrollment(); e.CourseID = k.ClassRecordID; e.StudentID = studentToUSe.UniqueRecordID; db.Enrollments.Add(e); db.SaveChanges(); } string toRedirect = "details/" + studentRecordID; return RedirectToAction(toRedirect); }
public ActionResult registerSelected(string[] ids, string classRecID) { string cRecID = ""; if (String.IsNullOrEmpty(classRecID)) { cRecID = Request.QueryString["uid"]; ViewData["classRecID"] = cRecID; } if (!(String.IsNullOrEmpty(classRecID))) { cRecID = classRecID; ViewData["classRecID"] = cRecID; } int courseKey = int.Parse(cRecID); List<Student> studentsToAdd = new List<Student>(); if (ids != null && ids.Length != 0) { foreach (string s in ids) { int sint = int.Parse(s); //this section makes sure you cant create an identical enrollment twice var checker = from r in db.Enrollments select r; checker = checker.Where(e => e.CourseID == courseKey && e.Student.UniqueRecordID == sint); if (!(checker.Any())) { // no Match! studentsToAdd.Add(db.Students.Find(sint)); } } } Course courseToUse = db.Courses.Find(courseKey); foreach (Student k in studentsToAdd) { Enrollment e = new Enrollment(); e.CourseID = courseToUse.ClassRecordID; e.StudentID = k.UniqueRecordID; db.Enrollments.Add(e); db.SaveChanges(); } string toRedirect = "details/" + cRecID; return RedirectToAction(toRedirect); }