Example #1
0
        public ActionResult Save(Movie movie)
        {
            //if model is not valid, return current form
            if (!ModelState.IsValid)
            {
                var viewModel = new NewMovieFormViewModel(movie)
                {
                    Genre = _context.Genres.ToList()
                };
                return(View("MovieForm", viewModel));
            }

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

                movieInDb.Name          = movie.Name;
                movieInDb.ReleaseDate   = movie.ReleaseDate;
                movieInDb.GenreId       = movie.GenreId;
                movieInDb.NumberInStock = movie.NumberInStock;
            }

            _context.SaveChanges();
            return(RedirectToAction("Index", "Movies"));
        }
        public ActionResult Save(Movie movie)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewMovieFormViewModel(movie)
                {
                    Genres = _context.Genres.ToList()
                };
            }

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

                movieInDb.Name          = movie.Name;
                movieInDb.ReleaseDate   = movie.ReleaseDate;
                movieInDb.GenreId       = movie.GenreId;
                movieInDb.NumberInStock = movie.NumberInStock;
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Movies"));
        }
Example #3
0
        public IActionResult Save(Movie movie)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewMovieFormViewModel(movie)
                {
                    Genres = _context.Genre.ToList()
                };

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

            if (movie.Id == 0)  // sprawdzamy czy movie jest nowy
            {
                _context.Movies.Add(movie);
            }
            else  // jeżeli nie wyszukujemy w bazie
            {
                var movieInDb = _context.Movies.Single(x => x.Id == movie.Id);

                // TryUpdateModelAsync(customer);
                //przypisyjemy ręcznie lub alternatywa  AutoMapper  Mapper.Map(customer,customerInDb)
                movieInDb.Name          = movie.Name;
                movieInDb.ReleaseDate   = movie.ReleaseDate;
                movieInDb.GenreId       = movie.GenreId;
                movieInDb.NumberInStock = movie.NumberInStock;
            }

            _context.SaveChanges();
            return(RedirectToAction("Index", "Movies"));
        }
Example #4
0
        public ActionResult Save(Movie movie)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewMovieFormViewModel
                {
                    Movie = movie
                };
                return(View("MovieForm", viewModel));
            }
            if (movie.Id == 0)
            {
                dbContext.Movies.Add(movie);
            }
            else
            {
                var movieInDb = dbContext.Movies.Single(x => x.Id == movie.Id);
                movieInDb.Name            = movie.Name;
                movieInDb.NumberAvailable = movie.NumberAvailable;
                movieInDb.NumberInStock   = movie.NumberInStock;
                movieInDb.ReleaseDate     = movie.ReleaseDate;
            }
            dbContext.SaveChanges();

            return(RedirectToAction("Index", "Movies"));
        }
Example #5
0
        public ActionResult Save(Movie movie) //<-- Model Binding
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewMovieFormViewModel(movie)
                {
                    MovieGenres = _context.Genres.ToList()
                };

                return(View("MovieForm", viewModel));
            }
            if (movie.Id == 0)
            {
                movie.DateAdded = DateTime.Now;
                _context.Movies.Add(movie); // Adds to memory only. DB context has a change tracking mechanism
            }
            else
            {
                var movieInDb = _context.Movies.Single(c => c.Id == movie.Id);
                movieInDb.Name          = movie.Name;
                movieInDb.MovieGenre    = movie.MovieGenre;
                movieInDb.ReleaseDate   = movie.ReleaseDate;
                movieInDb.NumberInStock = movie.NumberInStock;
            }

            _context.SaveChanges();                      // Persist the changes to DB

            return(RedirectToAction("Index", "Movies")); // Redirect User back to the list of customers
        }
Example #6
0
        public ActionResult New()
        {
            var viewModel = new NewMovieFormViewModel
            {
                Movie = new Movie()
            };

            return(View("MovieForm", viewModel));
        }
Example #7
0
        public ActionResult Create()
        {
            var vm = new NewMovieFormViewModel
            {
                Genre = _context.Genres.ToList()
            };

            return(View(vm));
        }
Example #8
0
        public ActionResult New()
        {
            var genres    = _context.Genres.ToList();
            var viewModel = new NewMovieFormViewModel
            {
                Genre = genres
            };

            return(View("MovieForm", viewModel));
        }
Example #9
0
        public ActionResult NewMovie()
        {
            var movieGenres = _context.Genres.ToList();
            NewMovieFormViewModel movieForm = new NewMovieFormViewModel()
            {
                Genre = movieGenres
            };

            return(View(movieForm));
        }
Example #10
0
        public ActionResult New(Movie movie)
        {
            var genre     = _context.Genres.ToList();
            var viewModel = new NewMovieFormViewModel(movie)
            {
                //Movie = new Movie(),
                Genres = genre
            };

            return(View("MovieForm", viewModel));
        }
Example #11
0
        public IActionResult Edit(int id)
        {
            var movie = _context.Movies.SingleOrDefault(x => x.Id == id);

            if (movie == null)
            {
                return(NotFound());
            }
            var viewModel = new NewMovieFormViewModel(movie)
            {
                Genres = _context.Genre.ToList()
            };

            return(View("MoviesForm", viewModel));
        }
Example #12
0
        public ActionResult Edit(int id)
        {
            var movie = dbContext.Movies.SingleOrDefault(c => c.Id == id);

            if (movie == null)
            {
                return(Content("Not found"));
            }

            var viewModel = new NewMovieFormViewModel
            {
                Movie = movie
            };

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

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

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


            return(View("MovieForm", viewModel));
        }
Example #14
0
        public ActionResult Edit(int id)
        {
            var movie = _context.Movies.Single(c => c.Id == id);

            if (movie == null)
            {
                return(HttpNotFound());
            }
            ;
            var vm = new NewMovieFormViewModel
            {
                Genre          = _context.Genres.ToList(),
                GenreId        = movie.GenreId,
                DateAdded      = movie.DateAdded,
                MovieTitle     = movie.MovieTitle,
                DateReleased   = movie.DateReleased,
                NumbersInStock = movie.NumbersInStock
            };

            return(View("create", vm));
        }
Example #15
0
        public ActionResult Edit(int id)
        {
            var movie = _context.Movies.SingleOrDefault(c => c.Id == id);

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

            var viewModel = new NewMovieFormViewModel(movie)
            {
                Id            = movie.Id,
                Name          = movie.Name,
                ReleaseDate   = movie.ReleaseDate,
                NumberInStock = movie.NumberInStock,
                GenreId       = movie.GenreId,

                Genres = _context.Genres.ToList()
            };

            return(View("MovieForm", viewModel));
        }
Example #16
0
        public ActionResult Save(Movie movie)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewMovieFormViewModel(movie)
                {
                    //Movie = movie,
                    Genres = _context.Genres.ToList()
                };
                return(View("MovieForm", viewModel));
            }


            if (movie.Id == 0)
            {
                movie.DateAdded = DateTime.Now;
                //New Movie Added

                _context.Movies.Add(movie);
            }
            else
            {
                var movieInDb = _context.Movies.Single(m => m.Id == movie.Id);

                /* Never Do This!  *** Opens security holes despite being Microsoft Preferred Solution */
                //TryUpdateModel(customerInDb, new string[] {"Name,"Email");

                movieInDb.Name          = movie.Name;
                movieInDb.GenreId       = movie.GenreId;
                movieInDb.ReleaseDate   = movie.ReleaseDate;
                movieInDb.DateAdded     = movie.DateAdded;
                movieInDb.NumberInStock = movie.NumberInStock;
            }


            _context.SaveChanges();
            return(RedirectToAction("Index", "Movies"));
        }
Example #17
0
        public ActionResult Create(NewMovieFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                var vm = new NewMovieFormViewModel
                {
                    Genre = _context.Genres.ToList()
                };
                return(View("create", vm));
            }

            if (viewModel.Id <= 0)
            {
                var movies = new Movies
                {
                    DateAdded      = viewModel.DateAdded,
                    DateReleased   = viewModel.DateReleased,
                    GenreId        = viewModel.GenreId,
                    MovieTitle     = viewModel.MovieTitle,
                    NumbersInStock = viewModel.NumbersInStock
                };
                _context.Movies.Add(movies);
                _context.SaveChanges();
            }
            else
            {
                var edit = _context.Movies.Single(c => c.Id == viewModel.Id);
                edit.NumbersInStock = viewModel.NumbersInStock;
                edit.DateAdded      = viewModel.DateAdded;
                edit.DateReleased   = viewModel.DateReleased;
                edit.GenreId        = viewModel.GenreId;
                edit.MovieTitle     = viewModel.MovieTitle;

                _context.SaveChanges();
            }
            return(RedirectToAction("Index"));
        }
Example #18
0
        public ActionResult Create(NewMovieFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                var movieGenres = _context.Genres.ToList();
                viewModel.Genre = movieGenres;
                return(View("NewMovie", viewModel));
            }

            var movieInDb = _context.Movies.FirstOrDefault(x => x.Id == viewModel.Movie.Id);

            if (movieInDb == null)
            {
                _context.Movies.Add(viewModel.Movie);
                _context.SaveChanges();
            }
            else
            {
                return(HttpNotFound());
            }


            return(Redirect("Index"));
        }