I have a C# server where I manually render some Razor views using the RazorEngine
NuGet library. The model supplied to the view is an anonymous type, created as follows:
new[] { Foo = "Bar", Baz = "Example" }
Because of the way I'm rendering my template, Visual Studio isn't aware of my model. This means that Visual Studio considers code like this invalid, even though it works fine:
<p>@Model.Foo</p>
To rectify this, I have discovered the @model
directive, but this appears only to work with classes as the specified model. For example, these are both considered invalid:
@model { string Foo, string Baz }
@model (string Foo, string Baz)
Is there any way I can get @model
to work with anonymous types? Alternatively, named tuples would satisfy the compiler too, since I never update any of my model's fields from the template.