渲染动作性能
我有显示问题、答案、对答案和问题的评论的视图。
要显示所有数据,我想使用这样的东西:
[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
。
I have view that display Question, Answers, Comments to answers and to Question.
To display all data i want to use something like this:
[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");
}
And at my partial views for example for question i will iterate in loop Model.AnswerIds
and call @{ Html.RenderAction("RenderAnswer", new {answerId}) };
and Model.CommentIds and call @{ Html.RenderAction("RenderComment", new {commentId}) };
I want to know, is it a good way of view decomposition and will this way have bad influence on performance caused often @Html.RenderAction
calls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,这会导致性能不佳。 RenderAction 并不以其惊人的速度而闻名。
您还将多次实例化控制器(也可能多次打开数据库)。
我建议您将所有内容放入一个专门的控制器操作中。
Unfortunately, this will cause bad performance. RenderAction is not known for its blazing speed.
You will also instantiate your controller multiple times (possibly opening the database multiple times too).
I recommend you put everything into a single specialized controller action.