public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //actors will be the object to edit Actors actors = db.Actors.Find(id); //model is a new model ViewModelEditActors var model = new ViewModelEditActors(); //IdValues allocates the actor ID model.IdValue = actors.ID; //Name allocates the actor Name model.Name = actors.Name; //Mini Bio allocates the actor MiniBio model.Minibio = actors.Minibio; //BD allocates the actor BD(Birth Date) model.BD = actors.BD; //Photograph allocates the actor Photograph model.Photograph = actors.Photograph; //IdsAllCha length definition model.IdsAllCha = new int[db.Charas.Count()]; //ListAllCha will contain every Characcter model.ListAllCha = db.Charas.ToList(); //------Characters that are already selected //var that will go through the Array int i = 0; //charactersAux is an aux List var charactersAux = new List <Characters> { }; //IdsCha legth definition model.IdsCha = new int[actors.CharacterList.Count()]; //each character will be added into the aux Variable foreach (var p in actors.CharacterList) { model.IdsCha[i] = p.ID; charactersAux.Add(p); i++; } // the Characters List will contain the aux values model.ListCha = charactersAux; if (actors == null) { return(HttpNotFound()); } //return the model return(View(model)); }
public ActionResult Edit(ViewModelEditActors actors, HttpPostedFileBase photo, DateTime date, String oldphoto, int valueButton) { //get the actor Actors newActor = db.Actors.Find(actors.IdValue); //define List for Model errors actors.ListAllCha = db.Charas.ToList(); actors.ListCha = newActor.CharacterList; //button verification if (valueButton == 1) { return(RedirectToAction("Create", "Characters")); } //--photo confirmations //define the photo name string photoName = "ActorPhoto" + newActor.ID; //will contain the photo Path string pathPhoto = ""; //if the photo is null then if (photo == null) { //returns the model state actors.Photograph = oldphoto; } else { //if the img format is jpg if (photo.ContentType == "image/jpeg") { //give the photo a name and a Path photoName = "ActorPhoto" + actors.IdValue + ".jpg"; pathPhoto = Path.Combine(Server.MapPath("~/Multimedia/Atores/"), photoName); System.IO.File.Delete(pathPhoto); actors.Photograph = photoName; photo.SaveAs(pathPhoto); } else { //if not the model state will be returned actors.BD = newActor.BD; ModelState.AddModelError("", "Invalid photo type"); return(View(actors)); } } //date confirmations if (date > DateTime.Now) { //if date is above todays date then the error will appear actors.BD = newActor.BD; ModelState.AddModelError("", "Invalid date"); return(View(actors)); } else { //if not then the date is valid actors.BD = date; } //List confirmation //get and define the char //------------------Put the ModelState //remove data foreach (var AllCharacters in actors.IdsAllCha) { Characters charac = db.Charas.Find(AllCharacters); if (newActor.CharacterList.Contains(charac)) { newActor.CharacterList.Remove(charac); } } //add the selected character to the CharacterList if (actors.IdsCha == null) { int i = 0; foreach (var Ch in newActor.CharacterList) { actors.IdsCha[i] = Ch.ID; } } foreach (var ch in actors.IdsCha.ToList()) { Characters charac = db.Charas.Find(ch); if (!newActor.CharacterList.Contains(charac)) { newActor.CharacterList.Add(charac); } } //ACTORS entries newActor.BD = actors.BD; newActor.ID = actors.IdValue; newActor.Minibio = actors.Minibio; newActor.Name = actors.Name; newActor.Photograph = actors.Photograph; if (ModelState.IsValid) { db.Entry(newActor).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(actors)); }