public async Task <IActionResult> Create([Bind("Id,Name,Photo")] Player players)
        {
            if (ModelState.IsValid)
            {
                char[] seperators = { '/' };
                if (Request.Form.Files.Count > 0)
                {
                    if
                    (
                        Request.Form.Files[0].ContentType.Split(seperators, StringSplitOptions.RemoveEmptyEntries)[0] == "image" &&
                        Request.Form.Files[0].Length <= 204800
                    )
                    {
                        players.Photo = await ImageUploadToBase64(Request.Form.Files[0]);
                    }
                    else
                    {
                        ModelState.AddModelError("Photo", "Photo is not a valid image type or is over 200 KiB.");
                        return(View(players));
                    }
                }

                _context.Add(players);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(players));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("Id,Batting,Bowling,GameId,PlayerId")] Stat stats)
        {
            if (ModelState.IsValid)
            {
                if (!StatsExistsForPlayerThisGame(stats.GameId, stats.PlayerId))
                {
                    _context.Add(stats);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Details", "Games", new { Id = stats.GameId }));
                }

                ModelState.AddModelError("PlayerId", "The selected player has already been assigned statistics for this game.");
            }
            return(Create(stats.Id));
        }
        public async Task <IActionResult> Create([Bind("Id,GameNumber,PlayerOfMatchId")] Game game)
        {
            if (game.PlayerOfMatchId == -1)
            {
                game.PlayerOfMatchId = null;
            }
            if (ModelState.IsValid)
            {
                if (!GameIsDuplicate(game.GameNumber))
                {
                    _context.Add(game);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }

                ModelState.AddModelError("GameNumber", "The game already exists.");
            }
            return(View(game));
        }