public async Task <IActionResult> Create(Slider slider) //model binding { if (ModelState["Photo"].ValidationState == ModelValidationState.Invalid) { return(View()); } if (!slider.Photo.IsImage()) { ModelState.AddModelError("Photo", "File type should be image"); return(View()); } if (!slider.Photo.IsLessThan(2)) { ModelState.AddModelError("Photo", "Image size can not be more than 2 mb."); return(View()); } string filename = await slider.Photo.SaveAsync(_env.WebRootPath, "sliders"); slider.Image = filename; await _context.Sliders.AddAsync(slider); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create(Destination destination) { if (!ModelState.IsValid) { return(View(destination)); } if (destination.Photo == null) { ModelState.AddModelError("Photo", "Photo should be selected"); return(View(destination)); } if (!destination.Photo.ContentType.Contains("image/")) { ModelState.AddModelError("Photo", "File type is not valid"); return(View(destination)); } if (destination.Photo.Length / 1024 / 1024 > 2) { ModelState.AddModelError("Photo", "File size can not be more than 2 mb"); return(View(destination)); } destination.Image = await destination.Photo.SaveAsync(_env.WebRootPath, "destinations"); await _context.Destinations.AddAsync(destination); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Edit([Bind(include: "Info, Title")] HoneyMoon honeyMoon) { if (!ModelState.IsValid) { return(View(honeyMoon)); } HoneyMoon honeyMoonFromDb = _context.HoneyMoon.First(); honeyMoonFromDb.Title = honeyMoon.Title; honeyMoonFromDb.Info = honeyMoon.Info; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }