Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a generic model in ASP.NET MVC Razor

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 
like image 723
egoodberry Avatar asked Apr 20 '11 19:04

egoodberry


People also ask

Does ASP.NET MVC use razor?

Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML. It's just that ASP.NET MVC has implemented a view engine that allows us to use Razor inside of an MVC application to produce HTML.

Can you mix MVC and Razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.

Should I use MVC or razor pages?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

Are razor pages Mvvm?

Razor Pages is sometimes described as implementing the MVVM (Model, View ViewModel) pattern. It doesn't. The MVVM pattern is applied to applications where the presentation and model share the same layer. It is popular in WPF, mobile application development, and some JavaScript libraries.


1 Answers

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

@model DtoViewModel<IDto> 

and take advantage of generic covariance

like image 139
Jack Avatar answered Oct 08 '22 22:10

Jack