Why can't my view's model bind with my generic ViewModel which implements an interface? (ASP.NET MVC 3)

前端 未结 2 1482
星月不相逢
星月不相逢 2021-01-18 07:06

I\'m trying to pass my View an instance of the following ViewModel:

public class CompanyListViewModel where T : ICompany
{
    public IEnumerable<         


        
2条回答
  •  -上瘾入骨i
    2021-01-18 07:18

    What you're trying to accomplish should be possible by using an Interface for the ViewModel class and declaring the Generic type as Covariant. Like this:

    interface ICompanyListViewModel where T : ICompany
    {
    }
    

    That way you could also get away with an abstract class instead of an interface for Company.

提交回复
热议问题