public IActionResult UpdateArea(AreaForm model) { if (ModelState.IsValid) { try { string imageUrl = UpdatedAreaImage(model); Area area = _context.Areas.Find(model.AreaId); string filepath = Path.Combine(webEnvironment.WebRootPath, "images/areas"); if (imageUrl != null) { var areaImgPath = Path.Combine(Directory.GetCurrentDirectory(), filepath, area.Image); if (System.IO.File.Exists(areaImgPath)) { System.IO.File.Delete(areaImgPath); } } area.Name = model.AreaName; area.ExpPerCatch = model.ExpPerCatch; area.LevelRequirement = model.LevelRequirement; area.Image = imageUrl == null ? area.Image : imageUrl; List <AreasPokemon> wildPokemon = new List <AreasPokemon>(); _context.AreaPokemon.Where(ap => ap.AreaId == area.AreaId).ToList().ForEach(a => _context.AreaPokemon.Remove(a)); if (model.PokemonIds != null) { foreach (int pokemonId in model.PokemonIds) { wildPokemon.Add(new AreasPokemon { AreaId = model.AreaId, PokemonId = pokemonId }); } foreach (AreasPokemon wildPoke in wildPokemon) { _context.Add(wildPoke); } } _context.SaveChanges(); return(RedirectToAction("Areas", "Admin")); } catch (Exception) { ModelState.AddModelError("Custom", "Area name already exists!"); return(View(model)); } } return(View(model)); }
private string UpdatedAreaImage(AreaForm model) { string imageUrl = null; if (model.UploadImage != null) { string uploadsFolder = Path.Combine(webEnvironment.WebRootPath, "images/areas"); imageUrl = "uploaded" + Guid.NewGuid().ToString() + "_" + model.UploadImage.FileName; string imagePath = Path.Combine(uploadsFolder, imageUrl); using (var fileStream = new FileStream(imagePath, FileMode.Create)) { model.UploadImage.CopyTo(fileStream); } } return(imageUrl); }
public IActionResult AddArea(AreaForm model) { if (ModelState.IsValid) { try { string imageUrl = UpdatedAreaImage(model); Area newArea = new Area { Name = model.AreaName, ExpPerCatch = model.ExpPerCatch, LevelRequirement = model.LevelRequirement, Image = imageUrl }; _context.Add(newArea); _context.SaveChanges(); List <AreasPokemon> wildPokemon = new List <AreasPokemon>(); if (model.PokemonIds != null) { foreach (int pokemonId in model.PokemonIds) { wildPokemon.Add(new AreasPokemon { AreaId = newArea.AreaId, PokemonId = pokemonId }); } foreach (AreasPokemon wildPoke in wildPokemon) { _context.Add(wildPoke); } } _context.SaveChanges(); return(RedirectToAction("Areas", "Admin")); } catch (Exception) { ModelState.AddModelError("Custom", "Area name already exists!"); return(View(model)); } } return(View(model)); }
public async Task <IActionResult> UpdateArea(int id) { Area area = _context.Areas.Find(id); var SelectedPokes = await _context.AreaPokemon.Where(p => p.AreaId == id).Select(a => a.PokemonId).ToListAsync(); List <Pokemon> allPokemon = await _context.Pokemon.ToListAsync(); AreaForm model = new AreaForm { AreaId = area.AreaId, AreaName = area.Name, ExpPerCatch = area.ExpPerCatch, ImageUrl = area.Image, LevelRequirement = area.LevelRequirement, AllPoke = allPokemon, PokemonIds = SelectedPokes }; return(View(model)); }
private void btnAreasForm_Click(object sender, EventArgs e) { AreaForm areaForm = new AreaForm(); areaForm.Show(); }