public ActionResult Create(RechercheCreateEditViewModel vm) { if (ModelState.IsValid) { vm.Recherche.Animal.Type = db.TypeAnimaux.Find(vm.TypeId); db.Animaux.Add(vm.Recherche.Animal); db.Recherches.Add(vm.Recherche); if (Authentification.EstConnecte()) { var sessionUtilisateur = Authentification.GetSessionUtilisateur(); var utilisateur = db.Utilisateurs.Find(sessionUtilisateur.Id); vm.Recherche.Animal.Maitre = utilisateur; } var photoService = new PhotosService(); var photosRecherche = photoService.Add(new PhotoRechercheViewModel { Photos = vm.ImageFilesRecherche, Recherche = vm.Recherche }); var photosAnimal = photoService.Add(new PhotoRechercheViewModel { Photos = vm.ImageFilesAnimal, Animal = vm.Recherche.Animal }); vm.Recherche.Photos = photosRecherche; vm.Recherche.Animal.Photos = photosAnimal; db.SaveChanges(); return(RedirectToAction("Index")); } return(RedirectToAction("Create")); }
public ActionResult Edit(RechercheCreateEditViewModel vm) { if (ModelState.IsValid) { var recherche = db.Recherches.Find(vm.Recherche.Id); if (recherche == null) { return(HttpNotFound()); } recherche.Active = vm.Recherche.Active; recherche.DerniereApparition = vm.Recherche.DerniereApparition; recherche.Description = vm.Recherche.Description; recherche.Localisation = vm.Recherche.Localisation; db.SaveChanges(); var photoService = new PhotosService(); var photosRecherche = photoService.UpdatePhotosRecherche(new PhotoRechercheViewModel { Photos = vm.ImageFilesRecherche, Recherche = recherche, }); var photosAnimal = photoService.UpdatePhotosAnimal(new PhotoRechercheViewModel { Photos = vm.ImageFilesAnimal, Animal = vm.Recherche.Animal, }); recherche.Photos.AddRange(photosRecherche.Select(p => db.Photos.Find(p.Id))); recherche.Animal.Photos.AddRange(photosAnimal.Select(p => db.Photos.Find(p.Id))); db.SaveChanges(); return(RedirectToAction("Details", new { id = recherche.Id })); } return(View(vm)); }