// GET: Drills/Create public async Task <IActionResult> Create() { var user = await GetCurrentUserAsync(); if (user == null) { return(NotFound()); } { //var drill = _context.Drills.FindAsync(); //instantiate new viewmodel var viewModel = new DrillCategory() { //i think Drill ='s null because the user is creating it, not the viewModel Drill = null, CategoryOptions = _context.Categories.Select(c => new SelectListItem { Value = c.CategoryId.ToString(), Text = c.CategoryName }).ToList() }; //ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryName"); return(View(viewModel)); } //return View(); }
// GET: Drills/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var drill = await _context.Drills.FindAsync(id); if (drill == null) { return(NotFound()); } var viewmodel = new DrillCategory() { Drill = drill, CategoryOptions = _context.Categories.Select(c => new SelectListItem { Value = c.CategoryId.ToString(), Text = c.CategoryName }).ToList() }; return(View(viewmodel)); }
public async Task <IActionResult> Create([Bind("Id,DrillName,DrillDescription,PlayersRequired,DateCreated,UserId,CategoryId")] Drill drill) { //don't fully understand why removing user/userId from ModelState here. ModelState.Remove("User"); ModelState.Remove("userId"); var user = await GetCurrentUserAsync(); drill.UserId = user.Id; if (ModelState.IsValid) { _context.Add(drill); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } var viewmodel = new DrillCategory() { Drill = null, CategoryOptions = _context.Categories.Select(c => new SelectListItem { Value = c.CategoryId.ToString(), Text = c.CategoryName }).ToList() }; return(View(viewmodel)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,DrillName,DrillDescription,PlayersRequired,DateCreated,CategoryId")] Drill drill) { if (id != drill.Id) { return(NotFound()); } //don't fully understand why removing user/userId from ModelState here. ModelState.Remove("User"); ModelState.Remove("userId"); var user = await GetCurrentUserAsync(); drill.UserId = user.Id; if (ModelState.IsValid) { try { _context.Update(drill); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DrillExists(drill.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } /*else do the work similar to create/ 'post' however drill is already defined, Drill = drill*/ var viewmodel = new DrillCategory() { Drill = drill, CategoryOptions = _context.Categories.Select(c => new SelectListItem { Value = c.CategoryId.ToString(), Text = c.CategoryName }).ToList() }; return(View(viewmodel)); }