Pass subclass of generic model to razor view

前端 未结 3 1004
有刺的猬
有刺的猬 2020-12-28 20:25

The Big Picture:

I have found what seems like a limitation of Razor and I am having trouble coming up with a good way around it.

3条回答
  •  一向
    一向 (楼主)
    2020-12-28 21:02

    The problem you are experiencing is not a Razor error, but a C# error. Try to do that with classes, and you'll get the same error. This is because the model is not BaseFooModel, but BaseFooModel, and an implicit conversion cannot happen between the two. Normally, in a program you'd have to do a conversion to do that.

    However, with .NET 4, introduced was contravariance and covariance, which sounds like the ability of what you are looking for. This is a .NET 4 feature only, and I honestly don't know if Razor in .NET 4 makes use of it or not.

提交回复
热议问题