This is a migrated thread and some comments may be shown as answers.

DropDownList for property with Enum

1 Answer 323 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Christopher
Top achievements
Rank 1
Christopher asked on 19 May 2017, 04:13 PM

This seems like it should be fairly simple and yet here I am.

I have a grid with inline editing.  I have set an editor template for a column to have a dropdown list.  This column is bound to a property that is set according to an enum.  All of that works great, and I get a dropdown with the names.

However, it always posts to the database as null.  All I want is the dropdown to show names when editing, post the value when creating or editing, then display the name when just viewing.

I have scoured forums but continue to come up empty.

Model

public class Issue
{
    public int Id { get; set; }
    ...
    ...
    public Urgency? Urgency { get; set; }
    ...
    ...
}

 

Enum

public enum Urgency
{
    Low = 1,
    Medium = 2,
    High = 3,
    Critical = 4
}

 

EditorTemplate View

@(Html.Kendo().DropDownList()
        .Name("Urgency")
        .BindTo(Enum.GetNames(typeof(Urgency)).ToList())
)

I have tried many other things but this is where my code is at the moment.

 

What am I missing?

1 Answer, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 22 May 2017, 01:34 PM
Hi Christopher,

Try to change your editor template with the following:

@(Html.Kendo().DropDownList()
    .Name("Urgency")
    .DataTextField("Text")
    .DataValueField("Value")
    .BindTo(EnumHelper.GetSelectList(Model.GetType(), Model)))

Please let me know if this works for you, in case the aforementioned approach doesn't solve your problem send me the project or a sample where the issue occurs so I can Investigate your exact scenario locally.


Regards,
Georgi
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
DropDownList
Asked by
Christopher
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Share this question
or