public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Writer writer = db.Writers.Find(id); if (writer == null) { return(HttpNotFound()); } //create a new Model var newWr = new ViewModelEditWriter(); //give the writer calues to the model newWr.IDValue = writer.ID; newWr.MiniBio = writer.MiniBio; newWr.Name = writer.Name; newWr.Photograph = writer.Photograph; newWr.Place_BD = writer.Place_DB; //give the length and all the Moves database List newWr.MovieAllFK = new int[db.Movies.Count()]; newWr.MovieAllList = db.Movies.ToList(); //get the defined List values int i = 0; var WRAux = new List <Movie> { }; newWr.MovieFK = new int[writer.MoviesList.Count()]; foreach (var p in writer.MoviesList) { newWr.MovieFK[i] = p.ID; WRAux.Add(p); i++; } newWr.MovieList = WRAux; return(View(newWr)); }
public ActionResult Edit(ViewModelEditWriter writer, String oldphoto, HttpPostedFileBase photo, DateTime date, int valueButton) { //get the Writer Writer wr = db.Writers.Find(writer.IDValue); // give the writer values to the model ro Modelstates writer.MovieAllList = db.Movies.ToList(); writer.MovieList = wr.MoviesList; //button verification if (valueButton == 1) { return(RedirectToAction("Create", "Movies")); } //--photo confirmations //photo name variable string photoName = "WriterPhoto" + wr.ID; //photo path variable string pathPhoto = ""; //photo validations and names if (photo == null) { writer.Photograph = wr.Photograph; } else { if (photo.ContentType == "image/jpeg") { photoName = photoName + ".jpg"; wr.Photograph = photoName; pathPhoto = Path.Combine(Server.MapPath("~/Multimedia/Writers/"), photoName); } else { writer.Place_BD = wr.Place_DB; ModelState.AddModelError("", "Invalid photo"); return(View(writer)); } } //date confirmations if (date > DateTime.Now) { writer.Place_BD = wr.Place_DB; ModelState.AddModelError("", "Invalid date"); return(View(writer)); } else { writer.Place_BD = date; } //List confirmation if (writer.MovieFK == null) { int i = 0; writer.MovieFK = new int[wr.MoviesList.Count()]; foreach (var mov in wr.MoviesList) { writer.MovieFK[i] = mov.ID; i++; } ModelState.AddModelError("", "No Movie Selected"); return(View(writer)); } //get and define the Movies //------------------Put the ModelState foreach (var allmov in writer.MovieAllFK) { Movie m = db.Movies.Find(allmov); if (wr.MoviesList.Contains(m)) { wr.MoviesList.Remove(m); } } //add data foreach (var mov in writer.MovieFK.ToList()) { Movie movie = db.Movies.Find(mov); if (!wr.MoviesList.Contains(movie)) { wr.MoviesList.Add(movie); } } //Director entries wr.Name = writer.Name; wr.MiniBio = writer.MiniBio; wr.ID = writer.IDValue; wr.Photograph = writer.Photograph; wr.Place_DB = writer.Place_BD; if (ModelState.IsValid) { db.Entry(wr).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(writer)); }