public ActionResult Save(Movie movie)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new MovieAddViewModel(movie)
                {
                    Genres = _context.Genres.ToList()
                };

                return(View("MovieForm", viewModel));
            }

            if (movie.Id == 0) // new movie
            {
                movie.DateAdded = DateTime.Now;
                _context.Movies.Add(movie);
            }
            else// existing movie
            {
                var movieInDb = _context.Movies.Single(m => m.Id == movie.Id);

                //TryUpdateModel(customerInDb); // Another approach, can open up a number of issues, security holes...

                //Instead of these 4 lines you can use a library like AutoMapper...
                //Mapper.Map(customer, customerInDb);
                movieInDb.Name          = movie.Name;
                movieInDb.GenreId       = movie.GenreId;
                movieInDb.ReleaseDate   = movie.ReleaseDate;
                movieInDb.NumberInStock = movie.NumberInStock;
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Movies"));
        }
Esempio n. 2
0
        public ActionResult Add()
        {
            var model = new MovieAddViewModel
            {
                Movie      = new Movie(),
                Categories = _categoryService.GetAll()
            };

            return(View(model));
        }
        public ActionResult New()
        {
            var genres    = _context.Genres.ToList();
            var viewModel = new MovieAddViewModel
            {
                Genres = genres
            };

            return(View("MovieForm", viewModel));
        }
        public ActionResult Edit(int id)
        {
            var movie = _context.Movies.SingleOrDefault(m => m.Id == id);

            if (movie == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new MovieAddViewModel(movie)
            {
                Genres = _context.Genres.ToList()
            };

            return(View("MovieForm", viewModel));
        }