// GET: Users/Details/5 public async Task <IActionResult> Details(int?id) { if (id == null) { return(RedirectToAction("Login")); } var user = await _context.Users.FirstOrDefaultAsync(m => m.UserId == id); if (user == null) { return(NotFound()); } _session.SetInt32("UserID", user.UserId); ViewDataVM vm = new ViewDataVM(); vm.CurrentUser = user; vm.Enrollments = _context.Enrollments.Where(x => x.User.UserId == user.UserId).ToList(); vm.Courses = new List <Course>(); foreach (var enrollment in vm.Enrollments) { vm.Courses.Add(_context.Courses.Where(x => x.CourseId == enrollment.CourseId).FirstOrDefault()); } return(View(vm)); }
// GET: Enrollments public async Task <IActionResult> Index() { var user = await _context.Users.Where(x => x.UserId == _session.GetInt32("CurrentUserId")).FirstOrDefaultAsync(); if (user != null) { ViewDataVM vm = new ViewDataVM(); vm.CurrentUser = user; vm.Courses = await _context.Courses.ToListAsync(); return(View(vm)); } else { return(RedirectToAction("Login", "Users")); } // var dataContext = _context.Enrollments.Include(e => e.Course).Include(e => e.User); // return View(await dataContext.ToListAsync()); }