ASP.NET MVC PartialView generic ModelView

Posted by Greg Ogle on Stack Overflow See other posts from Stack Overflow or by Greg Ogle
Published on 2010-05-26T21:52:18Z Indexed on 2010/05/27 0:21 UTC
Read the original article Hit count: 352

I have an ASP.NET MVC application which I want to dynamically pick the partial view and what data gets passed to it, while maintaining strong types.

So, in the main form, I want a class that has a view model that contains a generically typed property which should contain the data for the partial view's view model.

public class MainViewModel<T>
{
    public T PartialViewsViewModel { get; set; }
}

In the User Control, I would like something like:

Inherits="System.Web.Mvc.ViewUserControl<MainViewModel<ParticularViewModel>>" %>

Though in my parent form, I must put

Inherits="System.Web.Mvc.ViewPage<MainViewModel<ParticularViewModel>>" %>

for it to work.

Is there a way to work around this? The use case is to make the user control pluggable. I understand that I could inherit a base class, but that would put me back to having something like a dictionary instead of a typed view model.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about best-practices