// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Dishes.Add(Dish); await _context.SaveChangesAsync(); if (Image != null) { var fileName = $"{Dish.DishId}" + Path.GetExtension(Image.FileName); Dish.Image = fileName; var path = Path.Combine(_environment.WebRootPath, "Images", fileName); using (var fStream = new FileStream(path, FileMode.Create)) { await Image.CopyToAsync(fStream); } await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, enable the specific properties you want to bind to. // For more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(Dish).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DishExists(Dish.DishId)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Dish = await _context.Dishes.FindAsync(id); if (Dish != null) { _context.Dishes.Remove(Dish); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } string path = ""; // предыдущее изображение string previousImage = String.IsNullOrEmpty(Dish.Image) ? "" : Dish.Image; if (image != null) { // новый файл изображения Dish.Image = Dish.DishId + Path.GetExtension(image.FileName); // путь для нового файла изображения path = Path.Combine(_environment.WebRootPath, "images", Dish.Image); } _context.Attach(Dish).State = EntityState.Modified; try { await _context.SaveChangesAsync(); if (image != null) { // если было предыдущее изображение if (!String.IsNullOrEmpty(previousImage)) { // если файл существует var fileInfo = _environment.WebRootFileProvider .GetFileInfo("/Images/" + previousImage); if (fileInfo.Exists) { var oldPath = Path.Combine(_environment.WebRootPath, "images", previousImage); // удалить предыдущее изображение System.IO.File.Delete(oldPath); } } using (var stream = new FileStream(path, FileMode.Create)) { // сохранить новое изображение await image.CopyToAsync(stream); }; } } catch (DbUpdateConcurrencyException) { if (!DishExists(Dish.DishId)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }