public static async Task <bool> ProcessImages(ICollection <IFormFile> files, Pokemon pokemon, PokedexContext _context, IHostingEnvironment _environment, ModelStateDictionary m) { if (m.IsValid) { if (pokemon.ID == 0) { List <HarvestItem> h = pokemon.Harvestables.Where(harvestable => harvestable.IsHarvestable).ToList(); _context.Pokemon.Add(pokemon); } else { _context.Pokemon.Update(pokemon); } await _context.SaveChangesAsync(); var uploadDir = Path.Combine(_environment.WebRootPath, $"images\\pokemon\\{pokemon.Name.ToLower()}"); foreach (var file in files) { if (file.Length > 0) { var fileGuid = Guid.NewGuid(); if (!Directory.Exists(uploadDir)) { Directory.CreateDirectory(uploadDir); } var fsName = $"{fileGuid.ToString()}.{Path.GetExtension(file.FileName)}"; var filePath = Path.Combine(uploadDir, fsName); var pokemonImage = new PokemonImage() { Pokemon = pokemon, Active = true, Caption = "", ImageName = file.FileName, FileSystemName = fsName, PokemonID = pokemon.ID }; using (var fs = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(fs); } _context.PokemonImages.Add(pokemonImage); } } if (_context.ChangeTracker.HasChanges()) { await _context.SaveChangesAsync(); } return(true); } return(false); }
public async Task <IActionResult> Crear([Bind("Region")] Regiones regiones) { if (ModelState.IsValid) { _context.Add(regiones); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(regiones)); }
public async Task <IActionResult> Create([Bind("TiposId,NombreT")] Tipos tipos) { if (ModelState.IsValid) { _context.Add(tipos); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(tipos)); }
public async Task <IActionResult> Create([Bind("ID,AuthoredBy,Body,DatePosted,SubTitle,Title")] HomeContent homeContent) { if (ModelState.IsValid) { _context.Add(homeContent); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(homeContent)); }
public async Task <IActionResult> Create([Bind("PokeId,Nombre,Tipo1,Tipo2,Region,Move1,Move2,Move3,Move4,Foto")] Pokemon pokemon) { if (ModelState.IsValid) { _context.Add(pokemon); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["Region"] = new SelectList(_context.Regiones, "RegId", "NombreR", pokemon.Region); ViewData["Tipo1"] = new SelectList(_context.Tipos, "TiposId", "NombreT", pokemon.Tipo1); ViewData["Tipo2"] = new SelectList(_context.Tipos, "TiposId", "NombreT", pokemon.Tipo2); return(View(pokemon)); }
public async Task <IActionResult> Crear(TipoViewModel tipo) { if (ModelState.IsValid) { var tip = new Tipo { Tipos = tipo.Tipos }; _context.Add(tip); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(tipo)); }
public async Task <IActionResult> Create(PokemonViewModel pokemon) { if (ModelState.IsValid) { var pokemo = new Pokemon { Nombre = pokemon.Nombre, Tipo = pokemon.Tipo, Tipo2 = pokemon.Tipo2, Ataques = pokemon.Ataques, Ataque2 = pokemon.Ataque2, Ataque3 = pokemon.Ataque3, Ataque4 = pokemon.Ataque4, Region = pokemon.Region }; _contex.Add(pokemo); await _contex.SaveChangesAsync(); } return(RedirectToAction("Index")); }