This is a migrated thread and some comments may be shown as answers.
Problem binding enum to dropdownlist
2 Answers 276 Views
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 26 Apr 2013, 04:07 PM
I am trying to bind an enum to a dropdown list. I am successful using the vanilla @Html.DropDownListFor, but unsuccessful with the @Html.Kendu().DropDownListFor.

My enum provider is:
public static IEnumerable<SelectListItem> SexListItems
{
    get
    {
        foreach (var value in Enum.GetValues(typeof(Sex)))
        {
            string name = string.Format("Sex.{0}", Enum.GetName(typeof (Sex), value));
            var text = Resources.Shared.EnumStrings.ResourceManager.GetString(name);
            yield return new SelectListItem() {Value = value.ToString(), Text = text};
        }
    }
}


The following code works well, showing the list and binding to the  property::
@Html.DropDownListFor(p => p.Sex, DataProviders.SexListItems)


However, the following displays the list, but does not bind to the property:
               
@Html.Kendo().DropDownListFor(p => p.Sex).BindTo(ELSORegistry.DataProviders.SexListItems)

 

 

 


2 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 30 Apr 2013, 08:28 AM
Hello Steven,

When the field which you want to edit as an enum then we cannot determine whether the value is the string representation or the int representation of that enum.
For example in your case the SelectListItem value is actually the string (text) representation.
So you need to specify the value manually by turning the enum value into string.

e.g.

@Html.Kendo().DropDownListFor(m=>m.Sex).BindTo(YourProvider.SexListItems).Value(Model.Sex.ToString())


Kind Regards,
Petur Subev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Steve
Top achievements
Rank 1
answered on 30 Apr 2013, 04:12 PM
Many thanks. Petur, it works perfectly. Great product, just having to navigate the learning curve.
Asked by
Steve
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Steve
Top achievements
Rank 1
Share this question
or