public ActionResult Create() { //variable that will contain the Characters that are into the database var ch = db.Charas.ToList(); //new Model to be possible represent the fks var model = new ViewModelCreateFilmePerso(); //get the length necessary to put all the ids from the characters database model.idsCharacters = new int[db.Charas.Count()]; //define characters list model.Listcharacters = ch; //variable that will contain the Directors that are into the database var dr = db.Directores.ToList(); //get the length necessary to put all the ids from the Directors database model.idsDirectores = new int[db.Directores.Count()]; //define Directors list model.ListDirectors = dr; //variable that will contain the Writers that are into the database var wr = db.Writers.ToList(); //get the length necessary to put all the ids from the Writers database model.idsWriters = new int[db.Writers.Count()]; //define Writers list model.ListWriters = wr; return(View(model)); }
public ActionResult Create(ViewModelCreateFilmePerso movie, HttpPostedFileBase photo, DateTime date, int valueButton) { //-----valueButton "exception"------ if (valueButton == 1) { return(RedirectToAction("Create", "Characters")); } if (valueButton == 2) { return(RedirectToAction("Create", "Directors")); } if (valueButton == 3) { return(RedirectToAction("Create", "Writers")); } //new movie creation Movie newmovie = new Movie(); //define the movie ID int newID; //if we have 0 ids then the id will be one if (db.Movies.Count() == 0) { newID = 1; } else { //the id will be the higher ID value plus One newID = db.Movies.Max(a => a.ID) + 1; } //the id will be equals to the variable newmovie.ID = newID; //-----Lists inicialization ------ thats because the Lists are null and that will generate an error in the end---- //characters List inicialization var chr = db.Charas.ToList(); movie.Listcharacters = chr; //Directors list inicialization var drs = db.Directores.ToList(); movie.ListDirectors = drs; //Writers list inicialization var wrs = db.Writers.ToList(); movie.ListWriters = wrs; //This will verify the name, this statement is not necessary but in the begin an error occurred if (movie.Name == null) { ModelState.AddModelError("", "Name not found"); return(View(movie)); } // //define the photo Name string photoName = "MoviePhoto" + newID; //will define the photo path string pathPhoto = ""; if (photo == null) { movie.idsCharacters = new int[0]; movie.idsDirectores = new int[0]; movie.idsWriters = new int[0]; //photo cannot be null if that happens then this error will appear ModelState.AddModelError("", "Image not found"); return(View(movie)); } else { if (photo.ContentType == "image/jpeg") { //the content needs to be jpeg if not the site will reject it photoName = photoName + ".jpg"; newmovie.Photograph = photoName; pathPhoto = Path.Combine(Server.MapPath("~/Multimedia/Filme/"), photoName); } else { movie.idsCharacters = new int[0]; movie.idsDirectores = new int[0]; movie.idsWriters = new int[0]; //rejection of the photo ModelState.AddModelError("", "Invalid photo type"); return(View(movie)); } } //--see if the following fks are selected //Characters if (movie.idsCharacters == null) { movie.idsCharacters = new int[0]; movie.idsDirectores = new int[0]; movie.idsWriters = new int[0]; ModelState.AddModelError("", "No Characters selected "); return(View(movie)); } //Directors if (movie.idsDirectores == null) { movie.idsCharacters = new int[0]; movie.idsDirectores = new int[0]; movie.idsWriters = new int[0]; ModelState.AddModelError("", "No Directors selected "); return(View(movie)); } //Writers if (movie.idsWriters == null) { movie.idsCharacters = new int[0]; movie.idsDirectores = new int[0]; movie.idsWriters = new int[0]; ModelState.AddModelError("", "No Writers selected "); return(View(movie)); } //-----Date validation------ if (date == null) { //if date equals Null then the an error will appear ModelState.AddModelError("", "no date defined"); return(View(movie)); } else { if (date > DateTime.Now) { //if date is not below today's date an error will appear ModelState.AddModelError("", "Invalid date"); } else { //if not the movie receives the date value newmovie.dataDePub = date; } } //----change the trailer //string that will contain the Trailer string verNumbTrailer = movie.Trailer; //if Trailer characters are less then 32 then the trailer is not from youtube if (verNumbTrailer.Length > 32) { //var that will contain the trailer withou the youtube trailer reference var verTrailer = movie.Trailer.Substring(0, 32); //if the varTrailer is equals to https://www.youtube.com/watch?v= the Trailer can be added if (verTrailer.Equals("https://www.youtube.com/watch?v=")) { //trailers will be only from youtube so we replace the watch value to facilitate the user newmovie.Trailer = movie.Trailer.Replace("/watch?v=", "/embed/"); } else { //if not th trailler its not valid ModelState.AddModelError("", "Invalid Trailer"); return(View(movie)); } } else { //if not the trailer do not have even the length to be an youtube link ModelState.AddModelError("", "Invalid Trailer thats not even from Youtube"); return(View(movie)); } //define movie rating this one will start at zero for default newmovie.Rating = 0; //---Lists Values atribution //Characters newmovie.CharactersList = new List <Characters> { }; //this foreach will find the characters based on FKS ids foreach (var ch in movie.idsCharacters.ToList()) { Characters charac = db.Charas.Find(ch); //add the Character to the Character List newmovie.CharactersList.Add(charac); } //Directors newmovie.DirectorList = new List <Director> { }; //this foreach will find the Directors based on FKS ids foreach (var ch in movie.idsDirectores.ToList()) { Director dir = db.Directores.Find(ch); //add the Director to the Directors List newmovie.DirectorList.Add(dir); } //Writers newmovie.WriterList = new List <Writer> { }; //this foreach will find the Writers based on FKS ids foreach (var ch in movie.idsWriters.ToList()) { Writer writer = db.Writers.Find(ch); //add the Writer to the Writer List newmovie.WriterList.Add(writer); } //List (Comments,Rates) will be empty for defauult newmovie.Comments = new List <Comment> { }; newmovie.Rates = new List <Rate> { }; //Synopse will not be verified newmovie.sinopse = movie.sinopse; //name attribution newmovie.Name = movie.Name; if (ModelState.IsValid) { db.Movies.Add(newmovie); db.SaveChanges(); //Cover path photo.SaveAs(pathPhoto); return(RedirectToAction("Index")); } return(View(movie)); }