コード例 #1
0
        public ActionResult Create(GameGenresFormViewModel gameGenresFromViewModel)
        {
            HttpPostedFileBase file = Request.Files["ImageData"];
            //if (!ModelState.IsValid)
            //{
            //    var movieFormViewModel = new GameGenresFormViewModel()
            //    {
            //        Game = gameGenresFromViewModel.Game,
            //        Genres = _context.Genres.ToList()
            //    };

            //    return View("New", movieFormViewModel);
            //}

            var game = gameGenresFromViewModel.Game;

            game.Image  = ConvertToBytes(file);
            game.Genres = new List <Genre>();
            foreach (int selectedGenre in gameGenresFromViewModel.SelectedGenres)
            {
                game.Genres.Add(_context.Genres.SingleOrDefault(g => g.Id.Equals(selectedGenre)));
            }

            _context.Games.Add(game);
            _context.SaveChanges();

            return(RedirectToAction("Index", "Games"));
        }
コード例 #2
0
        public ActionResult New()
        {
            var gameGenresFromViewModel = new GameGenresFormViewModel()
            {
                Genres = _context.Genres.ToList()
            };

            return(View(gameGenresFromViewModel));
        }