public async Task <IActionResult> Edit(int id, CategorySubcategoryViewModel viewModel) { Place place = viewModel.Place; if (id != place.Id) { return(RedirectToAction("PageNotFound", "Home")); } ModelState.Remove("Place.User"); ModelState.Remove("Place.UserId"); ModelState.Remove("Category.Name"); var user = await GetCurrentUserAsync(); place.UserId = user.Id; if (ModelState.IsValid) { try { _context.Update(place); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PlaceExists(place.Id)) { return(RedirectToAction("PageNotFound", "Home")); } else { throw; } } return(RedirectToAction("Details", "Cities", new { id = place.CityId })); } var currentCatId = place.Subcategory.CategoryId; viewModel = new CategorySubcategoryViewModel() { Place = place, CategoryOptions = new SelectList(_context.Category, "Id", "Name", currentCatId), SubcategoryOptions = new SelectList(_context.Subcategory, "Id", "Name", place.SubcategoryId) }; return(View(viewModel)); }
// GET: Places/Edit/5 public async Task <IActionResult> Edit(int?id) { var user = await GetCurrentUserAsync(); var place = await _context.Place .Include(p => p.Subcategory) .FirstOrDefaultAsync(m => m.Id == id); if (place == null || id == null || place.UserId != user.Id) { return(RedirectToAction("PageNotFound", "Home")); } var currentCatId = place.Subcategory.CategoryId; var viewModel = new CategorySubcategoryViewModel() { Place = place, CategoryOptions = new SelectList(_context.Category, "Id", "Name", currentCatId), SubcategoryOptions = new SelectList(_context.Subcategory.Where(s => s.CategoryId == currentCatId), "Id", "Name", place.SubcategoryId) }; return(View(viewModel)); }