public async Task<ActionResult> Create(Choice choice) { var defaultTermId = _context.YearTerms.Where(c => c.IsDefault == true).First().YearTermId; var checkStudentId = _context.Choices.Where(c => c.StudentId == choice.StudentId); if (checkStudentId.Where(c => c.YearTermId == defaultTermId).Count() != 0) { ViewBag.FirstChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.FirstChoiceOptionId); ViewBag.FourthChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.FourthChoiceOptionId); ViewBag.SecondChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.SecondChoiceOptionId); ViewBag.ThirdChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.ThirdChoiceOptionId); ModelState.AddModelError(string.Empty, "You have already registered for this term"); return View(choice); } if (choice.FirstChoiceOptionId == choice.SecondChoiceOptionId || choice.FirstChoiceOptionId == choice.ThirdChoiceOptionId || choice.FirstChoiceOptionId == choice.FourthChoiceOptionId || choice.SecondChoiceOptionId == choice.ThirdChoiceOptionId || choice.SecondChoiceOptionId == choice.FourthChoiceOptionId || choice.ThirdChoiceOptionId == choice.FourthChoiceOptionId) { ViewBag.FirstChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.FirstChoiceOptionId); ViewBag.FourthChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.FourthChoiceOptionId); ViewBag.SecondChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.SecondChoiceOptionId); ViewBag.ThirdChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.ThirdChoiceOptionId); ModelState.AddModelError(string.Empty, "Cannot have duplicate selections!"); return View(choice); } choice.YearTermId = defaultTermId; choice.SelectionDate = DateTime.Now; _context.Choices.Add(choice); await _context.SaveChangesAsync(); return RedirectToAction("Index", "Home"); }
public async Task<ActionResult> Edit(int id, Choice choice) { if (_context.YearTerms.Where(c => c.IsDefault).Count() != 0) { choice.YearTermId = _context.YearTerms.Where(c => c.IsDefault == true).First().YearTermId; } try { if (choice.FirstChoiceOptionId == choice.SecondChoiceOptionId || choice.FirstChoiceOptionId == choice.ThirdChoiceOptionId || choice.FirstChoiceOptionId == choice.FourthChoiceOptionId || choice.SecondChoiceOptionId == choice.ThirdChoiceOptionId || choice.SecondChoiceOptionId == choice.FourthChoiceOptionId || choice.ThirdChoiceOptionId == choice.FourthChoiceOptionId) { ViewBag.FirstChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.FirstChoiceOptionId); ViewBag.FourthChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.FourthChoiceOptionId); ViewBag.SecondChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.SecondChoiceOptionId); ViewBag.ThirdChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title", choice.ThirdChoiceOptionId); ModelState.AddModelError(string.Empty, "Cannot have duplicate selections!"); return View(choice); } var studentId = _context.Choices.Where(c => c.ChoiceId == id).Select(c => c.StudentId).FirstOrDefault(); choice.ChoiceId = id; choice.StudentId = studentId; choice.SelectionDate = DateTime.Now; _context.Choices.Attach(choice); _context.Entry(choice).State = EntityState.Modified; await _context.SaveChangesAsync(); return RedirectToAction("Index"); } catch (Exception ex) { ModelState.AddModelError(string.Empty, "Unable to save changes."); Console.WriteLine(ex); } ViewBag.FirstChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title"); ViewBag.FourthChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title"); ViewBag.SecondChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title"); ViewBag.ThirdChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title"); return View(choice); }
public ActionResult Create() { Choice currentUser = new Choice(); currentUser.StudentId = User.GetUserName(); ViewBag.FirstChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title"); ViewBag.FourthChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title"); ViewBag.SecondChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title"); ViewBag.ThirdChoiceOptionId = new SelectList(_context.Options.Where(c => c.IsActive == true), "OptionId", "Title"); return View(currentUser); }