I use a generic class model - which is similar in concept to the approach suggested by Craig.
I kind of wish MS would create an overload to RenderPartial
to give us the same functionality. Just an additional object data
parameter would be fine.
Anyway, my approach is to create a PartialModel which uses generics so it can be used for all .ascx controls.
public class PartialControlModel<T> : ModelBase
{
public T ParentModel { get; set; }
public object Data { get; set; }
public PartialControlModel(T parentModel, object data) : base()
{
ParentModel = parentModel;
Data = data;
}
}
The .ascx control should inherit from the correct PartialControlModel
if you want the view to be strongly typed, which most likely you do if you've got this far.
public partial class ThumbnailPanel :
ViewUserControl<PartialControlModel<GalleryModel>>
Then you render it like this :
<% Html.RenderPartial("ThumbnailPanel",
new PartialControlModel<GalleryModel>(ViewData.Model, tag)); %>
Of course when you refer to any parent model items you must use this syntax :
ViewData.Model.ParentModel.Images
You can get the data and cast it to the correct type with :
ViewData.Model.Data
If anyone has a suggestion on how to improve the generics I'm using please let me know.