public IActionResult AddPhotos(int id) { var model = new AddPhotosViewModel(); model.TripId = id; return(View(model)); }
public async Task <IActionResult> AddPhotos(AddPhotosViewModel model) { var photos = _context.Photo.Where(x => x.TripId == model.TripId).ToList(); if (photos.Count < 6) { foreach (var file in model.Photos) { if (photos.Count <= 6) { var filename = Path.Combine(_webhost.WebRootPath, "Photos", file.FileName); string ext = Path.GetExtension(file.FileName); if (ext == ".jpg" || ext == ".png" || ext == ".JPG" || ext == ".PNG") { using (var uploading = new FileStream(filename, FileMode.Create)) { await file.CopyToAsync(uploading); var photo = new Photo() { Name = file.FileName, TripId = model.TripId }; _context.Photo.Add(photo); _context.SaveChanges(); } } else { model.Error = "Plik " + file.FileName + " jest nieprawidłowy. Proszę wskazać plik z rozszerzeniem JPG lub PNG"; return(View(model)); } } else { model.Error = "Do danej wycieczki dodano już 6 zdjęć. Przykro nam, nie możesz dodać więcej."; return(View(model)); } } return(RedirectToAction("ShowPhotos", new { id = model.TripId })); } model.Error = "Do danej wycieczki dodano już 6 zdjęć. Przykro nam, nie możesz dodać więcej."; return(View(model)); }