public ActionResult Create([Bind(Include = "Tutor,Student")] AssociateTutor associateTutor) { if (ModelState.IsValid) { ApplicationUser user = _db.Users.Find(associateTutor.Tutor.Id); Student student = _db.Students.Find(associateTutor.Student.Id); AssociateTutor newAssociateTutor = new AssociateTutor { Tutor = user, Student = student }; _db.AssociateTutors.Add(newAssociateTutor); try { _db.SaveChanges(); } catch (Exception ex) { Exception x = ex; } return(RedirectToAction("Index")); } else { return(RedirectToAction("Create", "AssociateTutors")); } }
public ActionResult DeleteConfirmed(int id) { AssociateTutor associateTutor = _db.AssociateTutors.Find(id); _db.AssociateTutors.Remove(associateTutor); _db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "Id")] AssociateTutor associateTutor) { if (ModelState.IsValid) { _db.Entry(associateTutor).State = EntityState.Modified; _db.SaveChanges(); return(RedirectToAction("Index")); } return(View(associateTutor)); }
public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } AssociateTutor associateTutor = _db.AssociateTutors.Find(id); if (associateTutor == null) { return(HttpNotFound()); } return(View(associateTutor)); }
public ActionResult GetStudents(string id) { var allStudents = _db.Students.OrderBy(s => s.FirstName).ToList(); var students = new List <Student>(); // Don't include student(s) for whom this tutor is already PrimaryTutor // Or for whom this tutor is already Associate Tutor. foreach (Student student in allStudents) { // Check Associate Tutors for duplication: using (var context = new SenecaContext()) { var sqlString = "SELECT * FROM AssociateTutor WHERE Student_Id = " + student.Id; sqlString += " AND Tutor_Id = '" + id + "'"; AssociateTutor existingAssociateTutor = null; try { existingAssociateTutor = context.Database.SqlQuery <AssociateTutor>(sqlString).Single(); } catch (Exception ex) { var x = ex; existingAssociateTutor = null; } if (existingAssociateTutor == null) { if (student.PrimaryTutor == null) { students.Add(student); } else { if (student.PrimaryTutor.Id != id) { students.Add(student); } } } } } String json = JsonConvert.SerializeObject(students, Formatting.Indented); return(Content(json, "application/json")); }
// GET: AssociateTutors/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } AssociateTutor associateTutor = _db.AssociateTutors.Find(id); if (associateTutor == null) { return(HttpNotFound()); } using (var context = new SenecaContext()) { var sqlString = "SELECT Tutor_Id FROM AssociateTutor WHERE Id = " + associateTutor.Id; var tutorId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); associateTutor.Tutor = _db.Users.Find(tutorId); sqlString = "SELECT Student_Id FROM AssociateTutor WHERE Id = " + associateTutor.Id; var studentId = context.Database.SqlQuery <int>(sqlString).FirstOrDefault(); associateTutor.Student = _db.Students.Find(studentId); } return(View(associateTutor)); }