public ConsoleGamesCreates GetCreateLink(int id)
 {
     using (var ctx = new ApplicationDbContext())
     {
         var model = new ConsoleGamesCreates();
         model.GameId = id;
         var query = ctx.Consoles.Select(e => new ConsoleChoiceItem {
             ConsoleName = e.Name, ConsoleId = e.Id, isLinked = false
         });
         model.ConsoleList = query.ToList();
         return(model);
     }
 }
Beispiel #2
0
        public ActionResult ConsoleLink(ConsoleGamesCreates model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            //var service = new ConsoleGameService();

            if (_consoleGameService.CreateConsoleGames(model))
            {
                TempData["SaveResult"] = "Your game was linked.";
                return(RedirectToAction("Index"));
            }
            ModelState.AddModelError("", "Game could not be linked.");
            return(View(model));
        }
        public bool CreateConsoleGames(ConsoleGamesCreates model)
        {
            var recordList = new List <ConsoleGame>();

            foreach (var choice in model.ConsoleList)
            {
                if (choice.isLinked)
                {
                    var newEntity = new ConsoleGame()
                    {
                        ConsoleId = choice.ConsoleId, GameId = model.GameId
                    };
                    recordList.Add(newEntity);
                }
            }
            using (var ctx = new ApplicationDbContext())
            {
                foreach (var consoleGame in recordList)
                {
                    ctx.ConsoleGames.Add(consoleGame);
                }
                return(ctx.SaveChanges() >= 1);
            }
        }