Dark Hippo
Dark Hippo

Reputation: 1265

Binding Enum Name to a DataTextField

I'm binding an IEnumerable object to a Kendo ComboBox using Razor, but having a little trouble populating the correct DataTextField value.

Model

public class LanguageInformation
    {
        public Languages Language { get; set; }
        public int LanguageId { get; set; }
    }

Languages Enum

public enum Languages
    {
        English = 1,
        Spanish = 2,
        French = 3,
        German = 4
    }

Razor

@(Html.Kendo().ComboBox()
    .Name("Language")
    .DataTextField("Language")
    .DataValueField("LanguageId")
)

(Note: data source is defined and returning data fine, just haven't included it above)

The issue I'm having is that the DataTextField. I want to Name of the Languages enum, but am at a loss as to how to return it.

I've tried a few different things such as

.DataTextField(Enum.GetName(typeof(Languages), "Language"))

But that results in a

value passed in must be an enum base

error

So, how to I return the name of the enum value as the DataTextField?

Upvotes: 1

Views: 774

Answers (1)

Jonathan Buchanan
Jonathan Buchanan

Reputation: 878

Kendo does not traditionally bind to Enums, but to lists instead. You can make a list out of an Enum with the following code.

@(Html.Kendo().ComboBox()
    .Name("Language")
    .BindTo(Enum.GetNames(typeof(Languages)).ToList()
)

Upvotes: 0

Related Questions