示例#1
0
        public ActionResult Create()
        {
            var newDirector = new ViewModelCreateDirector();

            newDirector.MovieAllFK   = new int[db.Movies.Count()];
            newDirector.MovieAllList = db.Movies.ToList();

            return(View(newDirector));
        }
示例#2
0
        public ActionResult Create(ViewModelCreateDirector director, HttpPostedFileBase photo, DateTime date, int valueButton)
        {
            //create a new Director
            var newDir = new Director();

            //give the director the ingo for modelStates
            director.MovieAllFK   = new int[db.Movies.Count()];
            director.MovieAllList = db.Movies.ToList();

            //button to redirect
            if (valueButton == 1)
            {
                return(RedirectToAction("Create", "Movies"));
            }

            //define the Director ID
            int newID;

            if (db.Directores.Count() == 0)
            {
                newID = 1;
            }
            else
            {
                newID = db.Directores.Max(a => a.ID) + 1;
            }
            newDir.ID = newID;



            //Name verification //not necessary
            if (director.Name == null)
            {
                ModelState.AddModelError("", "Name not found");
                return(View(director));
            }

            //define the photo Name
            string photoName = "DirectorPhoto" + newID;
            //will have the photo path
            string pathPhoto = "";


            if (photo == null)
            {
                //if fot is null then
                ModelState.AddModelError("", "Image not found");
                return(View(director));
            }
            else
            {
                if (photo.ContentType == "image/jpeg")
                {
                    //if photo is jpg than save it and « add it to the model
                    photoName         = photoName + ".jpg";
                    newDir.Photograph = photoName;
                    pathPhoto         = Path.Combine(Server.MapPath("~/Multimedia/Directores/"), photoName);
                }
                else
                {
                    //if the photo is invalid
                    ModelState.AddModelError("", "Invalid photo type");
                    return(View(director));
                }
            }
            //Date validation
            if (date < DateTime.Now)
            {
                newDir.Place_BD = date;
            }
            else
            {
                ModelState.AddModelError("", "Invalid Date");
                return(View(director));
            }

            //name attribution
            newDir.Name = director.Name;

            //bio attribution
            newDir.MiniBio = director.MiniBio;

            //Movies List  Validation/attribution
            if (director.MovieFK == null)
            {
                //director can be created without a Movie
                director.MovieFK = new int[0];
            }

            //----add movies
            //add an empty List to the model
            newDir.MovieList = new List <Movie> {
            };

            //each movie selected goes into the Movie Lisst
            foreach (var mov in director.MovieFK.ToList())
            {
                Movie movie = db.Movies.Find(mov);

                newDir.MovieList.Add(movie);
            }

            if (ModelState.IsValid)
            {
                db.Directores.Add(newDir);
                db.SaveChanges();
                //saves the photo
                photo.SaveAs(pathPhoto);
                return(RedirectToAction("Index"));
            }

            return(View(director));
        }