public ActionResult Add(PhotoDTO photo, HttpPostedFileBase file, string albumName) { try { if (ModelState.IsValid) { // TODO: Add insert logic here var fileName = Path.Combine(Server.MapPath("../Uploads"), DateTime.Now.Ticks.ToString() + file.FileName); file.SaveAs(fileName); _service.AddPhoto(albumName, photo, fileName); return RedirectToAction("Index", "Albums"); } return View(); } catch { return View(); } }
public void AddPhoto(string albumName, PhotoDTO photo, string fileName) { Photo newPhoto = new Photo { Description = photo.Description, Path = fileName, Rating = photo.Rating, Title = photo.Title, //InAlbum = _service.GetAlbumByName(albumName) }; Album album = GetAlbumByName(albumName); List<Photo> photoList = album.Photos; if(photoList != null) { photoList.Add(newPhoto); } else { photoList = new List<Photo> { newPhoto }; } album.Photos = photoList; _repo.Add<Photo>(newPhoto); _repo.SaveChanges(); }