forum

Home / DeveloperSection / Forums / How to map dropDownlist to enum in C#?

How to map dropDownlist to enum in C#?

Anonymous User 2154 01-Dec-2014

I have bound a drop down list to the enum of days of week like this: 

    private void BindDayOfWeek()
    {
        this.ddlDayOfWeek.DataSource = GetWeekDays();
        this.ddlDayOfWeek.DataBind();
    }
 
    private List<DayOfWeek> GetWeekDays()
    {
        return Enum.GetValues(typeof(DayOfWeek)).Cast<DayOfWeek>().ToList();
    }

Now I want to read the int value of the selected week day (from dropdown list) which was in enum DayOfWeek i.e. if I select "Sunday" from dropdown, I should be able to pick the int value of "Sunday" in the enum DaysOfWeek (NOT ddlDayOfWeek.selectedValue OR SelectedIndex) 

How can I do that without a switch and if (Which I think can be one way)?


Updated on 02-Dec-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By