I have a strongly-typed Partial View that takes a ProductImage and when it is rendered I would also like to provide it with some additional ViewData which I create dynamically in the containing page. How can I pass both my strongly typed object and my custom ViewData to the partial view with the RenderPartial call?

Note that this will override the default ViewData that all your other Views have by default. If you are adding anything to ViewData, it will not be in this new dictionary that you're passing to your partial view.

this can be very useful if you need to retain TemplateInfo.HtmlFieldPrefix in the child control which will be reset otherwise
– Simon_WeaverJul 6 '13 at 10:44

9

This is definitely the better solution. Losing the ViewData means that you lose the ModelState and all validations. +1
– Kevin FarrugiaFeb 7 '15 at 11:08

This seems to work only if you do not pass a TemplateInfo.HtmlFieldPrefix in your ViewDataDictionary. I can only pass a variable as ilustrated on the answer, or pass a TemplateInfo.HtmlFieldPrefix but not both.
– FeroxNov 24 '16 at 13:42

The easiest way to pass additional data is to add the data to the existing ViewData for the view as @Joel Martinez notes. However, if you don't want to pollute your ViewData, RenderPartial has a method that takes three arguments as well as the two-argument version you show. The third argument is a ViewDataDictionary. You can construct a separate ViewDataDictionary just for your partial containing just the extra data that you want to pass in.