Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: Possible to have multiple display templates for a type?

I have two views that show roughly the same data, but one is by client while the other is by project. Normally this would be great, as the same display template gets re-used across both views. However, I need the display of those items to be different when they list by client vs by project. However, they already have display templates defined. Is there some way for me to have two display templates for a single type?

edit

Ok, I forgot one important detail that makes this more complicated. While there are individual models (view models) that hold the items for each view, the items themselves are of mixed types (common base class). The display templates are for each of the types of items that can be in the list, so I can't use an attribute on the model.

I suppose I could make individual sub-models to wrap or replace the classes, but that's more duplication and work than I'd prefer.

like image 895
Jim Avatar asked Nov 21 '25 03:11

Jim


1 Answers

Does each view have it's own strongly typed view? If so create two different templates, then in each model reference them with the [UIHint] annotation.

Example:

public class ClientModel
{
     [UIHint("ClientDisplay")]
     public SharedDataModel sharedData { get; set;}
     //Other fields below
}

Then do the same thing for Project model. If you're currently using the same model between the two you could wrap them in separate new models and do the same thing.

From what you've asked I believe this is what you're trying to do, I had a little trouble following your question.

like image 149
ceasterday Avatar answered Nov 23 '25 20:11

ceasterday