Generic partial view: how to set a generic class as model?

10,833

Solution 1

It's not possible to do it like that. The reason is the .aspx will generate a class that you don't have much control on it and you can't add a generic parameter to it. I guess the most straightforward way is to pass it as object.

Solution 2

You could make all your model types that you would pass into this partial inherit from a base class/interface that establishes the basic behavior that would be used by this partial view and accept any object of that class/interface type, or just have the view accept any type of object and then use reflection to base your partial view behavior off of.

EXAMPLE:

public interface IDisplayModel
{
    string DisplayText{get;set;}
    string ImageUrl{get;set;}
    string AltText{get;set;}
}

public interface ITrustGrid<T> where T : IDisplayModel
{    
    IPagedList<T> Elements { get; set; }    
    IList<IColumn<T>> Columns { get; set; }    
    IList<string> Headers { get; }
}

<%@ Control Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<ITrustGrid<IDisplayModel>>" %>

Naturally, your IDisplayModel would vary based on your desired behavior. This would then allow you to pass in anything to this partial that implements this base interface to establish general behavior.

Solution 3

I agree with Mehrdad, as far as I know it isn't possible to make generic views. In one of my projects, I used an interface much like your one, and then passed delegate functions to the view that handle the specific rendering of each item.

For instance, I would have used a non-generic view data class with an additional field:

public interface ITrustGrid {
    IPagedList Elements { get; set; }
    IList<IColumn> Columns { get; set; }
    IList<string> Headers { get; }

    Func<object, string> ElementRenderer { get; }
}

In your main view you'll prepare the view data:

<%
ITrustGrid data = (ITrustGrid)ViewData["employeeGrid"];
data.ElementRenderer = new Func<object, string>(delegate(o) {
    var employee = (Employee)o;
    //render employee
    return html;
});

Html.RenderPartial("SimpleTrustGridViewer", data);
%>

While in your grid partial you'll process the grid as usual, and then call the delegate to render each single cell:

<%
foreach(var element in ViewData.Elements){
    %>
    <tr>
        <td><%=ViewData.ElementRenderer(element) %></td>
    </tr>
    <%
}
%>

Of course, the code above only renders a single cell for each element, you'll have to create a slightly more complex delegate to render multiple columns (or pass in an array of delegates, one for each column).

I think this would be one of the cleanest ways to do it, albeit a bit cumbersome.

Share:
10,833
Thomas Stock
Author by

Thomas Stock

I'm a .NET developer that focuses on web technologies and UI. I love working with JavaScript and I believe that this language will play a major role in future software development. My weapons of choice are: ASP.NET MVC (+ Web API) jQuery / AngularJS Umbraco CMS Xamarin (iOS Development) I also have a strong interest in: UX design / Usability Rock Climbing I'm not afraid of css and I think twitter is overrated :-) Specialties: Front-end, JavaScript, .NET

Updated on June 03, 2022

Comments

  • Thomas Stock
    Thomas Stock almost 2 years

    I'm trying to build a generic grid view in an ASP.NET MVC application.

    Let me explain with some code:

    public interface ITrustGrid<T>
    {
        IPagedList<T> Elements { get; set; }
        IList<IColumn<T>> Columns { get; set; }
        IList<string> Headers { get; }
    }
    

    This is an interface of a class that allows me to set columns and expressions in my controller.

    I pass implementations to a partial view like this:

    <% Html.RenderPartial("SimpleTrustGridViewer", ViewData["employeeGrid"] as TrustGrid<EmployeeInfoDTO>); %>
    

    The problem is that I can't figure out how to make the partial view that renders the grid generic.

    In other words, I want to turn this:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ITrustGrid<EmployeeInfoDTO>>" %>
    

    into something like this:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ITrustGrid<T>>" %>
    

    => How can I make my partial view generic in the most simple way?

    EDIT:

    I solved this by using a TrustGridBuilder that has a public TrustGrid GetTrustGrid() method which returns a non-generic TrustGrid. The TrustGrid contains strings instead of linq stuff. So I execute the linq in the GetTrustGrid() method and put the strings in a TrustGrid object.

    Thanks for everybody to help me on the right track.