2

我有显示问题、答案、对答案和问题的评论的视图。

要显示所有数据我想使用这样的东西:

    [HttpGet, ChildActionOnly]
    public PartialViewResult RenderQuestion(int questionId, int page, int pageSize, string sort)//For question display
    {
        var question = questionsService.GetQuestion(questionId);
        var author = userService.GetUser(question.AuthorId);
        var commentIds = commentService.GetTopCommentIds(questionId, int.MaxValue, CommentType.Question);
        var answerIds = answerService.GetAnswerIdsByQuestion(page, pageSize, questionId, sort);
        var model = new QuestionModel{ Question = question, Author = author, CommentIds = commentIds, AnswerIds = answerIds}
        return PartialView("_Question", model);
    }
    [HttpGet, ChildActionOnly]
    public PartialViewResult RenderAnswer(int answerId)
    {
        var answer = answerService.GetAnswer(answerId);
        var author = userService.GetUser(answer.AuthorId);
        var commentIds = commentService.GetTopCommentIds(answerId, int.MaxValue, CommentType.Answer);
        var model = new AnswerModel { Answer = answer, Author = author, CommentIds = commentIds};
        return PartialView("_Answer");
    }

    [HttpGet, ChildActionOnly]
    public PartialViewResult RenderComment(int commentId, CommentType commentType)
    {
        var comment = commentService.GetComment(commentId, commentType);
        var author = userService.GetUser(comment.AuthorId);
        var model = new CommentModel { Comment = comment, Author = author};
        return PartialView("_Comment");
    }

例如,在我的部分观点中,我将在循环中迭代Model.AnswerIds并调用@{ Html.RenderAction("RenderAnswer", new {answerId}) };和 Model.CommentIds 并调用@{ Html.RenderAction("RenderComment", new {commentId}) };

我想知道,这是一种视图分解的好方法吗?这种方法会对性能产生不良影响,导致经常@Html.RenderAction调用。

4

1 回答 1

2

不幸的是,这会导致性能不佳。RenderAction 以其惊人的速度而闻名。

您还将多次实例化您的控制器(也可能多次打开数据库)。

我建议您将所有内容都放在一个专门的控制器操作中。

于 2012-01-31T21:04:04.770 回答