Using a generic model in ASP.NET MVC Razor

Genericsasp.net Mvc-3Razor

Generics Problem Overview


Is it possible to use a generic model in ASP.NET MVC 3 (w/ Razor)? The following fails with a syntax error:

@model DtoViewModel<T> where T : IDto

Generics Solutions


Solution 1 - Generics

given that @model is expecting a type - not a type declaration you could use:

@model DtoViewModel<IDto>

and take advantage of generic covariance

Solution 2 - Generics

Such syntax is not supported by Razor, sorry.

Solution 3 - Generics

Assuming, you want to use a generic type in order to avoid code duplications in each view of ViewModel<T> you can do it this way:

1. create a view for the parts of ViewModel<T> that are unique to the view

ModelView.cshtml:

@model ViewModel<specificType>

@{Layout = "~/Views/Shared/Layout.cshtml";}
<h2 class="sub-header">Specific type view</h2>

2. create a view for the common parts, that should be rendered in each view of <T>

Grid.cshtml:

@{ var webGrid = new WebGrid(Model.PageItems); }

<div class="row" style="overflow: auto">
	@webGrid.GetHtml("table-striped", mode: WebGridPagerModes.All, firstText: "First", lastText: "Last")
</div>

Since it's a partial view, you don't need to declare the type of Model again. It will simply use the model you defined in the parent view, that renders it. The property IList<T> PageItems of your model, will remain strongly typed with <specificType>.

3. Don't forget to actually render the partial view of your common parts

ModelView.cshtml:

@RenderPage("~/Views/Shared/Grid.cshtml")

Solution 4 - Generics

This is not ideal, but it works and could get pretty creative with this pattern.

@model YourNameSpace.MyModel


public MyModel
{
    public MyGenericType<string> ModelAStuff {get;set;}
    public MyGenericType<int> ModelBStuff {get;set;}
    public MyGenericType<DateTime> ModelCStuff {get;set;}
}

public class MyGenericType<T>
{
  //use T how ever you like
  public T Color {get;set;}
  public T Year  {get;set;}
}

Solution 5 - Generics

I found this thread and did some further digging. Looks like the feature is on the 2.2.0 backlog. If anyone wants to get involved you can check out the issue on Github. https://github.com/aspnet/Mvc/issues/7152

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionegoodberryView Question on Stackoverflow
Solution 1 - GenericsJackView Answer on Stackoverflow
Solution 2 - GenericsmarcindView Answer on Stackoverflow
Solution 3 - GenericswodzuView Answer on Stackoverflow
Solution 4 - Genericschdev77View Answer on Stackoverflow
Solution 5 - GenericsLichtView Answer on Stackoverflow