0

I have a DropdownList in my View with stored Enum Values.

public enum UserRole
{
    ADMINISTRATOR = 1,
    APPROVER = 2,
    ORDER = 3
}

 @Html.DropDownListFor(model => model.New_User.Role , new SelectList(Enum.GetValues(typeof(soukohin.Models.Admin.UserRole))), "Select Role", new { @class = "form-control", @id = "new_role", @placeholder = "役割" , @onchange = "getRoleInformation(this)" })

after selecting the value, I want to get the Integer value of this from Javascript.

I tried this code, but the return value is the Name of Enum.

 function getRoleInformation(value) {
       alert(value.value);
    }

SAMPLE RESULT: ORDER

How can I get the integer value, not the name of the enum?

1 Answer 1

1

You are giving the ArrayList constructor basically an array of strings.

The following might work:

replace

new SelectList(Enum.GetValues(typeof(soukohin.Models.Admin.UserRole)))

with

new SelectList((new System.Collections.ArrayList(Enum.GetValues(typeof(soukohin.Models.Admin.UserRole)))).Cast<soukohin.Models.Admin.UserRole>().Select(e => new KeyValuePair<int,string>((int)e,e.ToString())).ToArray(),"Key","Value")
Sign up to request clarification or add additional context in comments.

Comments

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.