public async Task <IActionResult> AddIds(StudentCourseConnection studentCourseConnection, int courseId, int studentId) { studentCourseConnection.CourseId = courseId; studentCourseConnection.StudentId = studentId; try { context.Add(studentCourseConnection); await context.SaveChangesAsync(); } catch (Exception) { TempData["Error"] = "Error ocurred!/The student is already enrolled for this course!"; return(RedirectToAction("Index", "Courses")); } TempData["Success"] = "The student has been added"; return(RedirectToAction("Index", "Courses")); }
public async Task <IActionResult> AddIds(StudentCourseConnection studentCourseConnection, int courseId, int studentId) { studentCourseConnection.CourseId = courseId; studentCourseConnection.StudentId = studentId; Student student = await _context.Students.FirstOrDefaultAsync(x => x.Id == studentId); try { student.EnrollmentDate = DateTime.Now; _context.Add(studentCourseConnection); await _context.SaveChangesAsync(); } catch (Exception e) { TempData["Error"] = "Error ocurred!/The student is already enrolled for this course!"; return(RedirectToAction("Index", "Courses")); } TempData["Success"] = "The student has been added"; return(RedirectToAction("Index", "Courses")); }