Lets say I am writing a blog web application using MVC pattern. Typical layout for the main page of the blog application is - some kind of a post index in the main part, and aside there are some addition parts, like timeline, tag navigation panel, subscribing panel, etc. Those controls also appear on a single post view, and may appear on other views I have.
My question is - how should I handle those panels aside in my views and controllers. I see three approaches here:
Create a big viewmodel class that contains all the information required for rendering the view s (either index or single post). That case I could make these aside panels a partial views, and calling them from the view being rendered passing part of that big viewmodel. The drawback is - I will have the code filling viewmodel spread amongst various controller method, meaning code is duplicated. Which is quite bad.
Create another layer of view rendering. Say, the topmost layer of rendering recives a alreay rendered parts of html, or functions which when called outputs an html. The layer under this "combining partials layer" would give just partial views for each panel I want, include main content. The drawback here - memory usage. Most modern frameworks renders htmls directly to output stream, but in this approach partial views would be rendered into string objects first - which leads to memory overhead.
- Use something like "RenderAction" from asp.net mvc, which calls a controller method from a view. I think this is a worst solution of 3 given, because it drops an MVC approach.
The question is not tied to any specific framework, I want to understand the general way of doing things like that.
UPDATE
After an answer given, I've found out that post is not clear. So reasonable update here:
Under viewmodel term I understand an object that contains all the data required to render a particular view.
All of three approaches involve building partial views with their own viewmodel. For example (using C# syntax):
class SinglePostViewModel {
string Text {get;set;}
string Title {get;set;}
string Slug {get;set;}
DateTime PublishedDate {get;set;}
...
}
class TagNavigationPanelViewModel {
string TagText {get;set;}
int PostsCount {get;set;}
}
class CalendarNavigationPanelViewModel {
DateTime YearAndMonth {get;set;}
int PostsCount {get;set;}
}
My question is - how to nicely combine those partial views together.