public async Task <IActionResult> Edit(int id, [Bind("Afinacion,MarcaID,Modelo,Tipo,Material,NombreImagen,ImagenSubir,ID")] Trompeta trompeta) { if (id != trompeta.ID) { return(NotFound()); } string wwwRootPath = _hostEnvironment.WebRootPath; string pathActual = Path.Combine(wwwRootPath + "/Productos/", trompeta.NombreImagen); if (trompeta.ImagenSubir != null) { string fileName = Path.GetFileNameWithoutExtension(trompeta.ImagenSubir.FileName); string extension = Path.GetExtension(trompeta.ImagenSubir.FileName); trompeta.NombreImagen = fileName = fileName + extension; string path = Path.Combine(wwwRootPath + "/Productos/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await trompeta.ImagenSubir.CopyToAsync(fileStream); } } if (ModelState.IsValid) { try { _context.Update(trompeta); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TrompetaExists(trompeta.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["MarcaID"] = new SelectList(_context.Marcas, "ID", "Nombre", trompeta.MarcaID); return(View(trompeta)); }
public async Task <IActionResult> Create([Bind("Afinacion,MarcaID,Modelo,Tipo,Material,ImagenSubir,ID")] Trompeta trompeta) { if (ModelState.IsValid) { //Guardar IMG en wwwroot/Productos/ string wwwRootPath = _hostEnvironment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(trompeta.ImagenSubir.FileName); string extension = Path.GetExtension(trompeta.ImagenSubir.FileName); trompeta.NombreImagen = fileName = fileName + extension; string path = Path.Combine(wwwRootPath + "/Productos/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await trompeta.ImagenSubir.CopyToAsync(fileStream); } _context.Add(trompeta); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["MarcaID"] = new SelectList(_context.Marcas, "ID", "Nombre", trompeta.MarcaID); return(View(trompeta)); }