0

I have the enum value that I need to set in the dropdown, but I am not able to do so.

 public static List<SelectListItem> GetDraft(int selectedGroupId)
{
    List<SelectListItem> data = new List<SelectListItem>();
    // attempt 1 
    data.Add(new SelectListItem() { Value = Hello.SIM, Text = "Create New Draft" });
                                               ^^ I want the value to be "2", compile time error as Value can only be string

    // attempt 2 
    data.Add(new SelectListItem() { Value = Hello.SIM.ToString(), Text = "Create New Draft" });
                                               ^^ I want the value to be "2" but now it comes out to be string as "SIM"
    // below are some other values that I need to set and they are working as expected as they are not ENUM
    // some code has been removed for brevity
    foreach (dsSimSettings.DraftRow row in draftData.Rows)
    {
        data.Add(new SelectListItem() { Value = row.idDraft.ToString(), Text = row.strDescription });
    }
    return data;
}

public enum Hello
{
   SIM = 2
}

EDIT

[AuthorizeSettings]
        public JsonResult GetDrafts(int groupId)
        {
            List<SelectListItem> drafts = SecurityHelper.GetDraft(groupId);
            return Json(drafts);
        }
9
  • How/where do you use the GetDraft method ? Commented Jul 19, 2017 at 14:28
  • @Béranger: Added the edit Commented Jul 19, 2017 at 14:30
  • I just need one enum, that's it. Rest all dropdowns are fine. Just the first one needs to be ENUM and I want the value as "2". Commented Jul 19, 2017 at 14:31
  • Have you tried Hello.SIM.ToString() Commented Jul 19, 2017 at 14:31
  • @FailedUnitTest: Check my attempt 2, provided in the code snippet. it gives value as "SIM" and not "2" Commented Jul 19, 2017 at 14:32

1 Answer 1

1

You can cast the enum value using :

((int)Hello.SIM).ToString()
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.