Reputation: 6772
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.
Upvotes: 2
Views: 352
Reputation: 171178
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.
Upvotes: 2