2

I have created json variable called "grouptypejson" as mentioned below:-

var grouptypejson = {
    "opt": [
             {
                "n": "@SampleResource.GENERIC",
                "v": 0
             },
             {
                "n": "@SampleResource.DEPARTMENT",
                "v": 1
             }
           ]
}; 

The above json variable is defined globally in cshtml page under javascript body.

I am using hardcoded value i.e. "v":0 & "v":1

Instead of using hardcoded value, I want to get the number via enum which is defined under models namespace i.e. mentioned below:-

namespace My.Sample.Models.Enums
{
    public enum FilterEnum
    {
        Generic = 0,
        Department,
        All
    }
}

I want to read the value using the above enum and store the same at hard coded place

i.e. "v":0 & "v":1

I don't know how to get the enum value as integer and store at "v".

I tried using the following mentioned below but the result was Generic & Department respectively. Replaced 0 & 1 with the below string at the above json variable.

 "v":"@FilterEnum.GENERIC" & "v":"@FilterEnum.DEPARTMENT"

Please guide me how to do the same, I am including the enum namespace in my html page.

Please correct me if i have written anything wrong. I am beginner & learner.

Thanks in advance.

1 Answer 1

2

You can cast enum to int:

var grouptypejson = {
    "opt": [
             {
                "n": "@SampleResource.GENERIC",
                "v": @((int)SampleResource.GENERIC)
             },
             {
                "n": "@SampleResource.DEPARTMENT",
                "v": @((int)SampleResource.DEPARTMENT)
             }
           ]
}; 
Sign up to request clarification or add additional context in comments.

1 Comment

It is throwing an error: CS1525: Invalid expression term 'int'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.