Пример #1
0
 /// <summary>
 /// Views the article.
 /// </summary>
 /// <param name="articleId">The article id.</param>
 /// <returns>Page Content</returns>
 public ActionResult ViewArticle(int articleId)
 {
     ArticleViewModel viewModel = new ArticleViewModel
     {
         Article =
             this.articleRepository.Articles.SingleOrDefault(p => p.ArticleId == articleId),
         Comments = this.commentRepository.Comments.Where(c => c.ArticleId == articleId),
         NewComment = new Comment(articleId)
     };
     return this.View(viewModel);
 }
Пример #2
0
        public ActionResult ViewArticle(ArticleViewModel viewModel)
        {
            if (viewModel != null && viewModel.NewComment != null)
            {
                if (viewModel.NewComment.ArticleId == viewModel.Article.ArticleId)
                {
                    int nextId = 0;

                    if (commentRepository.Comments.Count() > 0)
                    {
                        var singleOrDefault = this.commentRepository.Comments.OrderByDescending(b => b.CommentId).Take(1).SingleOrDefault();
                        if (singleOrDefault != null)
                        {
                            nextId = singleOrDefault.CommentId + 1;
                        }
                    }
                    else
                    {
                        nextId = 1;
                    }

                    viewModel.NewComment.CommentId = nextId;

                    if (User.Identity.IsAuthenticated)
                    {
                        viewModel.NewComment.Username = User.Identity.Name;
                    }
                    else
                    {
                        viewModel.NewComment.Username = "******";
                    }

                    commentRepository.SaveComment(viewModel.NewComment);
                }
            }

            if (viewModel != null)
            {
                return
                    this.RedirectToRoute(
                        new { Controller = "Article", Action = "ViewArticle", ArticleId = viewModel.Article.ArticleId });
            }
            return this.View("Index");
        }