Exemple #1
0
        public ActionResult Create()
        {
            //create a new Model
            var newWriter = new ViewModelCreateWriter();

            //get the Fks ids
            newWriter.MovieAllFK = new int[db.Movies.Count()];
            //get the objects
            newWriter.MovieAllList = db.Movies.ToList();

            return(View(newWriter));
        }
Exemple #2
0
        public ActionResult Create(ViewModelCreateWriter writer, HttpPostedFileBase photo, DateTime date, int valueButton)
        {
            var newWriter = new Writer();

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

            if (valueButton == 1)
            {
                return(RedirectToAction("Create", "Movies"));
            }

            //define the writer ID
            int newID;

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



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

            //define the photo path
            string photoName = "WriterPhoto" + newID;
            string pathPhoto = "";


            if (photo == null)
            {
                ModelState.AddModelError("", "Image not found");
                return(View(writer));
            }
            else
            {
                if (photo.ContentType == "image/jpeg")
                {
                    photoName            = photoName + ".jpg";
                    newWriter.Photograph = photoName;
                    pathPhoto            = Path.Combine(Server.MapPath("~/Multimedia/Writers/"), photoName);
                }
                else
                {
                    ModelState.AddModelError("", "Invalid photo");
                    return(View(writer));
                }
            }
            //Date validation
            if (date < DateTime.Now)
            {
                newWriter.Place_DB = date;
            }
            else
            {
                ModelState.AddModelError("", "Invalid Date");
                return(View(writer));
            }
            //name and bio attribution
            newWriter.Name    = writer.Name;
            newWriter.MiniBio = writer.MiniBio;
            //Movies List  Validation/attribution
            if (writer.MovieFK == null)
            {
                //writer can be created without a Movie
                writer.MovieFK = new int[0];
            }
            //add a movie
            newWriter.MoviesList = new List <Movie> {
            };
            //each movie selected will be added into the MovieList
            foreach (var mov in writer.MovieFK.ToList())
            {
                Movie movie = db.Movies.Find(mov);

                newWriter.MoviesList.Add(movie);
            }


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

            return(View(writer));
        }