渲染动作表现

时间:2012-01-31 21:01:46

标签: asp.net-mvc asp.net-mvc-3 renderaction

我查看显示问题,答案,答案评论和问题。

要显示我想要使用的所有数据:

    [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次调用导致的性能产生不良影响。

1 个答案:

答案 0 :(得分:2)

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

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

我建议你把所有内容都放在一个专门的控制器动作中。