ASP.NET MVC3 DropDownList help

Hi people,

I am having a nightmare with getting a Dropdownlist control to work correctly in my MVC application.

I am trying to get a set of values out of an enumeration into SelectListItem which I then present to the DropDownList control as its source.

The issue is that I can get the thing to render but the selected value is never set and to top it off when the form is posted the value passed across is 0 instead of the actual value.

Any help is appreciated, below is an example of what I have.

In the View (Razor Code):

@Html.DropDownList("UserRole", new SelectList(Enum.GetValues(typeof(enumUserRoleUserManagement)), user.UserRole), new { @onchange = "submitForm(" + i + ")" })

in the Controller:

 ViewData["RL"] = new SelectList(userActionsModel.GenerateCombo().AsEnumerable(), "Value", "Text");

And Finally in the model:

public IEnumerable<SelectListItem> GenerateCombo()
        {
            var enumerationValues = Enum.GetValues(typeof(enumUserRoleUserManagement));
            var enumerationNames = Enum.GetNames(typeof(enumUserRoleUserManagement));

            List<SelectListItem> list = new List<SelectListItem>();

            foreach (var value in Enum.GetValues(typeof(enumUserRoleUserManagement)))
            {
                SelectListItem selectList = new SelectListItem
                {
                    Text = value.ToString(),
                    Value = value.ToString()
                };
                list.Add(selectList);
            }

            //SelectList selectList = new SelectList(list);

            return list;}

I have burned soooo much time on this, I will be indebted to anyone that provides a solution.

When you create your selectitem list, you should set the Selected property to true if it’s matching the model value.

Also see this