Using a generic model in ASP.NET MVC Razor

前端 未结 5 1724
刺人心
刺人心 2020-12-29 01:15

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

@model DtoViewModel where T : IDto
         


        
5条回答
  •  独厮守ぢ
    2020-12-29 01:54

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

    @model YourNameSpace.MyModel
    
    
    public MyModel
    {
        public MyGenericType ModelAStuff {get;set;}
        public MyGenericType ModelBStuff {get;set;}
        public MyGenericType ModelCStuff {get;set;}
    }
    
    public class MyGenericType
    {
      //use T how ever you like
      public T Color {get;set;}
      public T Year  {get;set;}
    }
    

提交回复
热议问题