public ActionResult Create(TrainerTopicViewModel model) { //get trainer var role = (from r in _context.Roles where r.Name.Contains("Trainer") select r).FirstOrDefault(); var users = _context.Users.Where(x => x.Roles.Select(y => y.RoleId).Contains(role.Id)).ToList(); //get topic var topics = _context.Topics.ToList(); if (ModelState.IsValid) { _context.TrainerTopics.Add(model.TrainerTopic); _context.SaveChanges(); return(RedirectToAction("Index")); } var TrainerTopicVM = new TrainerTopicViewModel() { Topics = topics, Trainers = users, TrainerTopic = new TrainerTopic() }; return(View(TrainerTopicVM)); }
public ActionResult Create(TrainerTopicViewModel model) { var role = (from r in _context.Roles where r.Name.Contains("Trainer") select r).FirstOrDefault(); var users = _context.Users.Where(x => x.Roles.Select(y => y.RoleId).Contains(role.Id)).ToList(); var topics = _context.Topics.ToList(); if (!ModelState.IsValid) { return(View()); } var trainerTopics = _context.TrainerTopics.ToList(); var topicId = model.TrainerTopic.TopicId; var checkTrainerInCourse = trainerTopics .SingleOrDefault(c => c.TopicId == topicId && c.TrainerId == model.TrainerTopic.TrainerId); if (checkTrainerInCourse != null) { return(RedirectToAction("Create")); } var traineeCourse = new TrainerTopicViewModel() { Topics = topics, Trainers = users, TrainerTopic = new TrainerTopic() }; _context.TrainerTopics.Add(model.TrainerTopic); _context.SaveChanges(); return(RedirectToAction("index")); }
public ActionResult Create() { var viewModel = new TrainerTopicViewModel { Topics = _context.Topics.ToList(), Trainers = _context.Users.ToList() }; return(View(viewModel)); }
public ActionResult Edit(int id) { var trainerInDb = _context.TrainerTopics.SingleOrDefault(trainerinDb => trainerinDb.Id == id); if (trainerInDb == null) { return(HttpNotFound()); } var viewModel = new TrainerTopicViewModel { TrainerTopic = trainerInDb, Topics = _context.Topics.ToList(), }; return(View(viewModel)); }
public ActionResult Create() { var role = (from r in _context.Roles where r.Name.Contains("Trainer") select r).FirstOrDefault(); var users = _context.Users.Where(x => x.Roles.Select(y => y.RoleId).Contains(role.Id)).ToList(); var topics = _context.Topics.ToList(); var TrainerTopicVM = new TrainerTopicViewModel() { Topics = topics, Trainers = users, TrainerTopic = new TrainerTopic() }; return(View(TrainerTopicVM)); }
public async Task <IActionResult> Create(TrainerTopicViewModel model) { var trainerRoleId = await _db.Roles.Where(t => t.Name == SD.Trainer).Select(t => t.Id).FirstAsync(); var listTrainerId = await _db.UserRoles .Where(t => t.RoleId == trainerRoleId) .Select(t => t.UserId) .ToArrayAsync(); List <ApplicationUser> trainerUsers = await _db.ApplicationUsers .Where(t => listTrainerId .Any(name => name.Equals(t.Id))) .ToListAsync(); if (ModelState.IsValid) { var doesTrainerTopicExists = _db.TrainerTopics.Include(t => t.Topic).Include(t => t.User) .Where(t => t.Topic.Id == model.TrainerTopic.TopicId && t.User.Id == model.TrainerTopic.TrainerId); if (doesTrainerTopicExists.Count() > 0) { //Error StatusMessage = "Error: The Trainer has bene assigned under " + doesTrainerTopicExists.First().Topic.Name + " topic. Please assigned another trainer."; } else { _db.TrainerTopics.Add(model.TrainerTopic); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } TrainerTopicViewModel modelVM = new TrainerTopicViewModel() { Topics = await _db.Topics.ToListAsync(), TrainerTopic = model.TrainerTopic, StatusMessage = StatusMessage, Users = trainerUsers }; return(View(modelVM)); }
public ActionResult Create() { { //Get trainer var role = (from r in _context.Roles where r.Name.Contains("Trainer") select r) .FirstOrDefault(); var users = _context.Users.Where(x => x.Roles.Select(y => y.RoleId) .Contains(role.Id)) .ToList(); //Get Topic var topics = _context.Topics.ToList(); var newTrainerTopic = new TrainerTopicViewModel { Topics = topics, Trainers = users, ManageTrainer = new ManageTrainer() }; return(View(newTrainerTopic)); } }
//GET :: CREATE public async Task <IActionResult> Create() { var trainerRoleId = await _db.Roles.Where(t => t.Name == SD.Trainer).Select(t => t.Id).FirstAsync(); var listTrainerId = await _db.UserRoles .Where(t => t.RoleId == trainerRoleId) .Select(t => t.UserId) .ToArrayAsync(); List <ApplicationUser> trainerUsers = await _db.ApplicationUsers .Where(t => listTrainerId .Any(name => name.Equals(t.Id))) .ToListAsync(); TrainerTopicVM = new TrainerTopicViewModel() { Topics = await _db.Topics.ToListAsync(), Users = trainerUsers, TrainerTopic = new Models.TrainerTopic(), }; return(View(TrainerTopicVM)); }