I declared an enum:
public enum HeightTypes{ Tall, Short}
Now I want to use it on my razor page like this:
@if (Model.Meta.Height == HeightTypes.Tall)
But there's a problem as I get an error. Is there some way I can tell the razor page about my enum?
Just do give a start-to-finish example:
C# CS Page
Razor View
You aren't specific about the exception, so I'm guessing this is a namespace issue. Add
at the top. You can also specify namespaces to add automatically in
/Views/web.config
if you are going to use that namespace a lot:You have an error in your enum declaration (remove the trailing
;
):then the following test should work:
you just have to make sure that your view is strongly typed and that you have brought into scope the namespace in which the Height enum is defined:
or reference the enum like this:
But to avoid doing this in all your razor views that require using this enum, it is easier to declare it in the
<namespaces>
section in the~/Views/web.config
: