// GET: Directors/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //finds the director Director director = db.Directores.Find(id); if (director == null) { return(HttpNotFound()); } //creates a new Model var newDir = new ViewModelEditDirector(); //values definition---- newDir.IDValue = director.ID; newDir.MiniBio = director.MiniBio; newDir.Name = director.Name; newDir.Photograph = director.Photograph; newDir.Place_BD = director.Place_BD; //MovieFk will be the array with all the Movies ids newDir.MovieAllFK = new int[db.Movies.Count()]; //MovirAllList List will have the database newDir.MovieAllList = db.Movies.ToList(); //i will be an auxiliar int int i = 0; //dirAux is an auviliar List<Movie> var dirAux = new List <Movie> { }; //MovieFK newDir.MovieFK = new int[director.MovieList.Count()]; //each director will have the selected movies foreach (var p in director.MovieList) { newDir.MovieFK[i] = p.ID; dirAux.Add(p); i++; } //Movie Lis is equals to the auxiliar var newDir.MovieList = dirAux; //retuins the model return(View(newDir)); }
public ActionResult Edit(ViewModelEditDirector director, String oldphoto, HttpPostedFileBase photo, DateTime date, int valueButton) { //get the director Director dir = db.Directores.Find(director.IDValue); director.MovieAllList = db.Movies.ToList(); director.MovieList = dir.MovieList; //button verification if (valueButton == 1) { return(RedirectToAction("Create", "Movies")); } //--photo confirmations //photo name variable string photoName = "DirectorPhoto" + dir.ID; //photo path variable string pathPhoto = ""; //photo validations and names /*This will verify the photo this one needs to be jpg and not null*/ if (photo == null) { director.Photograph = oldphoto; } else { if (photo.ContentType == "image/jpeg") { photoName = "ActorPhoto" + director.IDValue + ".jpg"; pathPhoto = Path.Combine(Server.MapPath("~/Multimedia/Directores/"), photoName); System.IO.File.Delete(pathPhoto); director.Photograph = photoName; photo.SaveAs(pathPhoto); } else { director.Place_BD = dir.Place_BD; ModelState.AddModelError("", "Invalid photo type"); return(View(director)); } } //date confirmations if (date > DateTime.Now) { //if date is invalid director.Place_BD = dir.Place_BD; ModelState.AddModelError("", "Invalid date"); return(View(director)); } else { //if date is valid director.Place_BD = date; } //List confirmation if (director.MovieFK == null) { int i = 0; director.MovieFK = new int[dir.MovieList.Count()]; foreach (var mov in dir.MovieList) { director.MovieFK[i] = mov.ID; i++; } ModelState.AddModelError("", "No Movie Selected"); return(View(director)); } //get and define the Movies //------------------Put the ModelState foreach (var allmov in director.MovieAllFK) { Movie m = db.Movies.Find(allmov); if (dir.MovieList.Contains(m)) { dir.MovieList.Remove(m); } } //add data foreach (var mov in director.MovieFK.ToList()) { Movie movie = db.Movies.Find(mov); if (!dir.MovieList.Contains(movie)) { dir.MovieList.Add(movie); } } //Director entries dir.Name = director.Name; dir.MiniBio = director.MiniBio; dir.ID = director.IDValue; dir.Photograph = director.Photograph; dir.Place_BD = director.Place_BD; if (ModelState.IsValid) { db.Entry(dir).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(director)); }