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")); }
public ActionResult New() { var gameGenresFromViewModel = new GameGenresFormViewModel() { Genres = _context.Genres.ToList() }; return(View(gameGenresFromViewModel)); }