On my start page, I’d like to display the first items from several different lists that I have on other pages – somewhat like the ‘recent’ page here on SO displays both recent posts and recent comments. In my case I want to list the two most recent posts in a guest book, and the next upcoming event.
In order to do this, how can I pass several Model objects to my View? Is it even possible? If not, how should it be done?
An alternative to tvanfosson’s solution is to create a strongly-typed view so that you get cvompile-time checking and less ‘magic strings.’
Example…
Assume you have the class:
Then in your controller…
And, finally… in your view. This will allow you to access the passed in view data with intellisense if you setup the view registration line up to use it (notice the part. This means the Model property will be an instance of the passed in viewdata argument into the VIew method in your controller.
Of course this is just a demo and I wouldn’t use this code verbatim.