Example #1
0
        public async Task <IActionResult> Edit([FromRoute] int ID, [FromForm] EditBoardViewModel editBoardViewModel, [FromRoute] int pageNumber)
        {
            TryValidateModel(editBoardViewModel);
            if (ID != editBoardViewModel.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                Board board = await mDbContext.Boards.FirstOrDefaultAsync(m => m.ID == ID);

                board.Subject   = editBoardViewModel.Subject;
                board.Content   = editBoardViewModel.Content;
                board.WriteDate = editBoardViewModel.WriteDate;
                try
                {
                    mDbContext.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!boardExists(editBoardViewModel.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index), "Boards", new { pageNumber }));
            }

            return(View(editBoardViewModel));
        }
        public async Task <IActionResult> Create([FromQuery] int pageNumber, [FromForm] CreateCommentViewModel createCommentViewModel)
        {
            TryValidateModel(createCommentViewModel);
            if (ModelState.IsValid)
            {
                Comment comment = new Comment()
                {
                    BoardID         = createCommentViewModel.BoardID,
                    CommentUserName = createCommentViewModel.CommentUserName,
                    CommentContent  = createCommentViewModel.CommentContent
                };
                mDbContext.Comments.Add(comment);
                await mDbContext.SaveChangesAsync();

                Board board = mDbContext.Boards.FirstOrDefault(m => m.ID == comment.BoardID);
                board.CommentCount = mDbContext.Comments.Count(m => m.BoardID == comment.BoardID);
                mDbContext.SaveChanges();
                return(RedirectToAction("Details", "Boards", new { ID = comment.BoardID, pageNumber }));
            }

            return(View(createCommentViewModel));
        }