public ActionResult AddTheUserToTheCourse(AllUsersAndSomeCourseViewModel model) { if (ModelState.IsValid) { using (_db) { var newConnection = _db.userCourse.Create(); string theUserId = model.SelectedUserId; newConnection.userId = theUserId; if (theUserId.IsNullOrWhiteSpace()) { return(Redirect("/Course/AddToCourse/" + model.TheCourse.Id)); } var allUsersInCourse = _courseService.GetUsersInSomeCourse(model.TheCourse.Id); foreach (var user in allUsersInCourse) { //Check if user is already in the course if (theUserId == user.userId) { return(Redirect("/Course/AddToCourse/" + model.TheCourse.Id)); } } newConnection.userId = theUserId; newConnection.courseId = model.TheCourse.Id; _db.userCourse.Add(newConnection); _db.SaveChanges(); } } //Redirect back to the same page return(Redirect("/Course/AddToCourse/" + model.TheCourse.Id)); }
public ActionResult AddToCourse(int?id) { int realId; if (id != null) { realId = (int)id; } else { throw new ArgumentNullException(); } AllUsersAndSomeCourseViewModel allUsersAndSomeCourseViewModel = new AllUsersAndSomeCourseViewModel() { TheCourse = _courseService.GetCourseById(realId), ListOfUsers = _db.Users.ToList(), AllUsersInCourse = _courseService.GetUsersInSomeCourse(realId) }; return(View(allUsersAndSomeCourseViewModel)); }