Search code examples
asp.net-mvcasp.net-mvc-3razor

Can I access a C# enum in my Razor view?


I have the following:

namespace Storage.Constants.References {
    public enum RoleType {
        Guest = 1,
        User = 2,
        Admin = 3,
        Super = 4
    }
}

In my web.config I added:

<add namespace="Storage.Constants.References" />

However in my view below I still don't get any intellisense for the RoleType and it appears to not know what it is:

@model WebUx.ViewModels.BaseViewModel
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }
@section status {

    @if (Model.Role >= RoleType.Admin)
    {

However it works if I change this to:

@if (Model.Role >= Storage.Constants.References.RoleType.Admin)

Should it not work without my specifying "Storage.Constants.References" ?


Solution

  • Did you try adding the namspace to your razor view ?

    @using Storage.Constants.References
    

    Also, if you are adding it in the web config file, make sure you are adding to the webconfig present in the Views folder, not the root level web config