Example #1
0
        public async Task <IActionResult> Create(
            [Bind("ID", "BoardGameID", "IsActive",
                  "ImageFilePath", "FormFileUpload")] Headlines headline)
        {
            var errors = ModelState
                         .Where(x => x.Value.Errors.Count > 0)
                         .Select(x => new { x.Key, x.Value.Errors })
                         .ToArray();

            if (ModelState.IsValid)
            {
                var filepath = "/images/headline/defaultHeadline/gray.jpg";
                if (headline.FormFileUpload != null)
                {
                    var rand     = new Random();
                    var randNum  = rand.Next(100000, 1000000);
                    var fileName = randNum.ToString() + "_" + headline.FormFileUpload.FileName;
                    filepath = $"/images/headline/{fileName}";
                    var file = Path.Combine(_environment.ContentRootPath, "wwwroot/images/headline/", fileName);

                    using (var fileStream = new FileStream(file, FileMode.Create))
                    {
                        await headline.FormFileUpload.CopyToAsync(fileStream);
                    }
                }

                var emptyHeadlines = new Headlines();

                if (await TryUpdateModelAsync <Headlines>(
                        emptyHeadlines,
                        "headline",
                        i => i.BoardGameID,
                        i => i.IsActive,
                        i => i.ImageFilePath))
                {
                    emptyHeadlines.ImageFilePath = filepath;
                    _db.Add(emptyHeadlines);
                    await _db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }


            IEnumerable <BoardGame> boardGames = await _db.BoardGames
                                                 .OrderBy(i => i.Title)
                                                 .ToListAsync();

            HeadlinesViewModel headlinesViewModel = new HeadlinesViewModel()
            {
                Headline   = headline,
                BoardGames = boardGames
            };

            return(View(headlinesViewModel));
        }
Example #2
0
        public async Task <IActionResult> Create()
        {
            Headlines headline = new Headlines();

            IEnumerable <BoardGame> boardGames = await _db.BoardGames
                                                 .OrderBy(i => i.Title)
                                                 .ToListAsync();

            HeadlinesViewModel headlinesViewModel = new HeadlinesViewModel()
            {
                Headline   = headline,
                BoardGames = boardGames
            };

            return(View(headlinesViewModel));
        }