Exemple #1
0
        public ActionResult Create(AssignTraineetoCourseViewModel assign)
        {
            var traineeInDb = (from te in _context.Roles where te.Name.Contains("Trainee") select te).FirstOrDefault();
            var traineeUser = _context.Users.Where(u => u.Roles.Select(us => us.RoleId).Contains(traineeInDb.Id)).ToList();
            var course      = _context.Courses.ToList();

            if (ModelState.IsValid)
            {
                var checkTraineeAndCourseExist = _context.AssignTraineetoCourses.Include(t => t.Course).Include(t => t.Trainee)
                                                 .Where(t => t.Course.ID == assign.AssignTraineetoCourse.CourseID && t.Trainee.Id == assign.AssignTraineetoCourse.TraineeID);
                //GET CourseID and TraineeID from the Course and Trainee tables in the ViewModel

                if (checkTraineeAndCourseExist.Count() > 0)                 //list ID comparison, if count == 0. jump to else
                {
                    ModelState.AddModelError("", "Assign Already Exists");
                }
                else
                {
                    _context.AssignTraineetoCourses.Add(assign.AssignTraineetoCourse);
                    _context.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            AssignTraineetoCourseViewModel traineecourseVM = new AssignTraineetoCourseViewModel()
            {
                Courses  = course,
                Trainees = traineeUser,
                AssignTraineetoCourse = assign.AssignTraineetoCourse
            };

            return(View(traineecourseVM));
        }
Exemple #2
0
        public ActionResult Create()
        {
            var traineeInDb = (from te in _context.Roles where te.Name.Contains("Trainee") select te).FirstOrDefault();
            // Get User role name Trainee and return
            var traineeUser = _context.Users.Where(x => x.Roles.Select(y => y.RoleId).Contains(traineeInDb.Id)).ToList();
            // Get User in table and select the ID containing the TraineeID
            var courses = _context.Courses.ToList();

            var viewModel = new AssignTraineetoCourseViewModel
            {
                Courses  = courses,
                Trainees = traineeUser,
                AssignTraineetoCourse = new AssignTraineetoCourse()
            };

            return(View(viewModel));
        }