public ActionResult PrintPartialViewToPdf(int id) { if (CheckCookies()) { var courseInDb = _context.Courses.Single(c => c.CourseId == id); var doctorCourseId = _context.DoctorCourses.Single(c => c.CourseId == courseInDb.CourseId).UserId; var stuffIds = _context.StuffCourses.Where(c => c.CourseId == courseInDb.CourseId) .Select(c => new { c.UserId }).ToList(); List <string> stuffNames = new List <string>(); foreach (var stuff in stuffIds) { string temp = ""; temp = _context.Users.Single(c => c.UserId == stuff.UserId).FirstName + " " + _context.Users.Single(c => c.UserId == stuff.UserId).LastName; stuffNames.Add(temp); } var studentIds = _context.Registrations.Where(c => c.CourseId == courseInDb.CourseId) .Select(c => new { c.UserId }).ToList(); List <StudentCourseDetVm> obj = new List <StudentCourseDetVm>(); foreach (var student in studentIds) { StudentCourseDetVm tempObj = new StudentCourseDetVm(); string studentId = _context.Users.Single(c => c.UserId == student.UserId).StudentID; string name = _context.Users.Single(c => c.UserId == student.UserId).FirstName + " " + _context.Users.Single(c => c.UserId == student.UserId).LastName; string email = _context.Users.Single(c => c.UserId == student.UserId).Email; var levelId = _context.Users.Single(c => c.UserId == student.UserId).AspStudentLvlId; string level = _context.StudentLvl.Single(c => c.Id == levelId).lvlName; tempObj.Id = studentId; tempObj.Email = email; tempObj.Name = name; tempObj.Level = level; obj.Add(tempObj); } CourseDetailsVm model = new CourseDetailsVm { CourseId = courseInDb.CourseId, Coursename = courseInDb.CourseName, CourseDepartment = _context.Departments.Single(c => c.DepId == courseInDb.AspDepartmentId).Name, CourseCode = courseInDb.CourseTitle, CourseCredit = "3", CourseTime = _context.CourseTimes.Single(c => c.Id == courseInDb.CourseTimeId).Time, CourseDoctor = _context.Users.Single(c => c.UserId == doctorCourseId).FirstName + " " + _context.Users.Single(c => c.UserId == doctorCourseId).LastName, CourseStuffs = stuffNames, students = obj }; var report = new PartialViewAsPdf("~/Views/Shared/_CourseResult.cshtml", model); return(report); } return(RedirectToAction("Logout", "User")); }
public ActionResult Details(int id) { if (CheckCookies()) { Session["ID"] = id; var courseInDb = _context.Courses.Single(c => c.CourseId == id); int doctorCourseId; string doctorName = ""; if (_context.DoctorCourses.Any(x => x.CourseId == courseInDb.CourseId)) { doctorCourseId = _context.DoctorCourses.Single(c => c.CourseId == courseInDb.CourseId).UserId; doctorName = _context.Users.Single(c => c.UserId == doctorCourseId).FirstName + " " + _context.Users.Single(c => c.UserId == doctorCourseId).LastName; } else { doctorName = "No Doctor Assiged For this Course Until Now"; } List <string> stuffNames = new List <string>(); List <StudentCourseDetVm> obj = new List <StudentCourseDetVm>(); if (_context.StuffCourses.Any(c => c.CourseId == courseInDb.CourseId)) { var stuffIds = _context.StuffCourses.Where(c => c.CourseId == courseInDb.CourseId) .Select(c => new { c.UserId }).ToList(); foreach (var stuff in stuffIds) { string temp = ""; temp = _context.Users.Single(c => c.UserId == stuff.UserId).FirstName + " " + _context.Users.Single(c => c.UserId == stuff.UserId).LastName; stuffNames.Add(temp); } } if (_context.Registrations.Any(z => z.CourseId == courseInDb.CourseId)) { var studentIds = _context.Registrations.Where(c => c.CourseId == courseInDb.CourseId) .Select(c => new { c.UserId }).ToList(); foreach (var student in studentIds) { StudentCourseDetVm tempObj = new StudentCourseDetVm(); string studentId = _context.Users.Single(c => c.UserId == student.UserId).StudentID; string name = _context.Users.Single(c => c.UserId == student.UserId).FirstName + " " + _context.Users.Single(c => c.UserId == student.UserId).LastName; string email = _context.Users.Single(c => c.UserId == student.UserId).Email; var levelId = _context.Users.Single(c => c.UserId == student.UserId).AspStudentLvlId; string level = _context.StudentLvl.Single(c => c.Id == levelId).lvlName; tempObj.Id = studentId; tempObj.Email = email; tempObj.Name = name; tempObj.Level = level; obj.Add(tempObj); } } var viewModel = new CourseDetailsVm { CourseId = courseInDb.CourseId, Coursename = courseInDb.CourseName, CourseDepartment = _context.Departments.Single(c => c.DepId == courseInDb.AspDepartmentId).Name, CourseCode = courseInDb.CourseTitle, CourseCredit = "3", CourseTime = _context.CourseTimes.Single(c => c.Id == courseInDb.CourseTimeId).Time, CourseDoctor = doctorName, CourseStuffs = stuffNames, students = obj }; return(View(viewModel)); } return(RedirectToAction("Logout", "User")); }