public async Task <IActionResult> Create(Enjoy enjoy) { if (ModelState["Title"].ValidationState == ModelValidationState.Invalid || ModelState["Photo"].ValidationState == ModelValidationState.Invalid) { return(View()); } if (!enjoy.Photo.IsImage()) { ModelState.AddModelError("Photo", "Shekil novunu duz secin"); return(View()); } if (!enjoy.Photo.CheckImageSize(2)) { ModelState.AddModelError("Photo", "Shekil hecmi coxdur"); return(View()); } string filename = await enjoy.Photo.CopyImage(_env.WebRootPath, "slider"); enjoy.Image = filename; await _context.Enjoys.AddAsync(enjoy); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Update(int?id) { if (id == null) { return(NotFound()); } Enjoy enjoy = await _context.Enjoys.FindAsync(id); if (enjoy == null) { return(NotFound()); } return(View(enjoy)); }
public async Task <IActionResult> DeletePost(int?id) { Enjoy enjoy = await _context.Enjoys.FindAsync(id); if (enjoy == null) { return(RedirectToAction(nameof(Index))); } Utility.DeleteImgFromFolder(_env.WebRootPath, enjoy.Image); _context.Enjoys.Remove(enjoy); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Update(int?id, Enjoy enjoy) { Enjoy enjoyDb = await _context.Enjoys.FindAsync(id); if (enjoyDb == null) { return(RedirectToAction(nameof(Index))); } if (ModelState["Title"].ValidationState == ModelValidationState.Invalid ) { return(View(enjoyDb)); } if (enjoy.PhotoUpdate != null) { if (!enjoy.PhotoUpdate.IsImage()) { ModelState.AddModelError("Photo", "Content type must be image"); return(View()); } if (!enjoy.PhotoUpdate.CheckImageSize(2)) { ModelState.AddModelError("Photo", "Image size not more than 2 Mb"); return(View()); } string filename = await enjoy.PhotoUpdate.CopyImage(_env.WebRootPath, "slider"); Utility.DeleteImgFromFolder(_env.WebRootPath, enjoyDb.Image); enjoyDb.Image = filename; } enjoyDb.Title = enjoy.Title; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }