I know my question is stupid, but I dont know solution of my problem and can understand similar questions on stackoverflow.
I doing simple blog.
And when I go to one post in this blog I must see text of post and comments for him. They there are in my datebase, but I dont know how display both.
Please help me
I know my question is stupid, but I dont know solution of my problem
Share
You can create a custom ViewModel for this particular View. Something like this:
Then you’d bind to that ViewModel for the View. The Controller action would get the Models it needs and build an instance of the ViewModel to pass to the View.
Another option would be to use a
Tuple. It’s a generic class which acts as a strongly-typed container for multiple other types. So the View’s Model would be something like this:From an overall design perspective, my biggest recommendation would be to consider how your Models relate to one another and find your “aggregate root.” In the case of a blog post with comments, it sounds like the post should be the aggregate root. The Model itself should have the comments within it. Something like this:
The idea is that the aggregate root is the parent object and internally knows about its child objects. You shouldn’t have to manually compose those hierarchies of objects every time you want to use them.