// GET: Collectibles/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var user = await _userManager.GetUserAsync(HttpContext.User); var viewModel = new CollectibleEditViewModel() { Collections = await _context.Collections.Where(c => c.User.Id == user.Id).ToListAsync(), Tags = await _context.Tags.ToListAsync() }; var collectible = await _context.Collectibles.FindAsync(id); if (collectible == null) { return(NotFound()); } else { viewModel.Collectible = collectible; } return(View(viewModel)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Description,CollectedDate,CollectionId")] CollectibleEditViewModel viewModel) { if (id != viewModel.Collectible.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var user = await _userManager.GetUserAsync(HttpContext.User); _context.Update(viewModel.Collectible); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CollectibleExists(viewModel.Collectible.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } //ViewData["CollectionId"] = new SelectList(_context.Collections, "Id", "Name", collectible.CollectionId); return(View(viewModel)); }
public async Task <IActionResult> Edit(int id, CollectibleEditViewModel viewModel) { if (id != viewModel.Collectible.Id) { return(NotFound()); } if (ModelState.IsValid) { try { viewModel.Collectible.CollectibleTags = viewModel.SelectedTagIds.Select(t => new CollectibleTag() { TagId = t, CollectibleId = id }).ToList(); _context.Update(viewModel.Collectible); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CollectibleExists(viewModel.Collectible.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(viewModel)); }