// GET: Actors/Create public ActionResult Create() { //gets the database characters in List format var ch = db.Charas.ToList(); //creates a new model to alocate the characters var model = new ViewModelCreateActors(); //gets the array length model.IdsAllCha = new int[db.Charas.Count()]; //allocates the Characters List model.ListAllCha = ch; //retuens the model return(View(model)); }
public ActionResult Create(ViewModelCreateActors Actor, HttpPostedFileBase photo, DateTime Date, int valueButton) { //adds the list to the param bc this param will be empty var p = db.Charas.ToList(); Actor.ListAllCha = p; // verify the button value to redirect Actor.ListAllCha = db.Charas; if (valueButton == 1) { return(RedirectToAction("Create", "Characters")); } //new actor creation Actors newActor = new Actors(); //define the Actor ID int newID; if (db.Actors.Count() == 0) { newID = 1; } else { newID = db.Actors.Max(a => a.ID) + 1; } newActor.ID = newID; //List inicialization var chr = db.Charas.ToList(); Actor.ListCha = chr; //Name verification //thas not really necessary tbh if (Actor.Name == null) { ModelState.AddModelError("", "Name not found"); return(View(Actor)); } //define the photo name string photoName = "ActorPhoto" + newID; //will contain the photo Path string pathPhoto = ""; //if the photo is null then if (photo == null) { //returns the model state ModelState.AddModelError("", "Image not found"); return(View(Actor)); } else { //if the img format is jpg if (photo.ContentType == "image/jpeg") { //give the photo a name and a Path photoName = photoName + ".jpg"; newActor.Photograph = photoName; pathPhoto = Path.Combine(Server.MapPath("~/Multimedia/Atores/"), photoName); } else { //if not the model state will be returned ModelState.AddModelError("", "Invalid photo type"); return(View(Actor)); } } //Date validation if (Date < DateTime.Now) { newActor.BD = Date; } else { ModelState.AddModelError("", "Invalid Date"); return(View(Actor)); } //name attribution newActor.Name = Actor.Name; //MiniBio attribution newActor.Minibio = Actor.Minibio; //CharacerList Validation/attribution if (Actor.IdsCha == null) { ModelState.AddModelError("", "No characters selected"); return(View(Actor)); } //--Add the characters to the List //charactersAux was created to add values var CharacterAux = new List <Characters> { }; foreach (var charac in Actor.IdsCha) { //ch1 is the character object Characters ch1 = db.Charas.Find(charac); //adds the ch1 to the Characters aux variable CharacterAux.Add(ch1); } //characterList will receive the characterAux Values newActor.CharacterList = CharacterAux; //ModelState if (ModelState.IsValid) { db.Actors.Add(newActor); db.SaveChanges(); //saves the photo Path photo.SaveAs(pathPhoto); return(RedirectToAction("Index")); } return(View(Actor)); }