DropDownList with Enum Kendo UI

I am working on updating an application to use the Kendo UI and have encountered a problem with binding to Enum with DropDownList. Two problems that I have are: 1) the value does not contain the Enum value and instead contains "Today" (should be 0) and 2) the displayed value is always "Last10Days" instead of "Last 10 Days" in the tag description. I looked and could not find another place where someone used the Kendo user interface to display the description as text and included a numerical value instead of text. Any help is appreciated.

View

<div class="span6">
  @Html.LabelFor(m=> m.DateRanges)
  @(Html.Kendo().DropDownListFor(m => m.DateRanges)
      .BindTo(Enum.GetNames(typeof(SearchDateRanges)).ToList())
      .HtmlAttributes(new { value = "Today" })
      .DataTextField("Text")
      .Events(e => e.Change("DateChange")))
</div>

<div class="span6">
  @Html.LabelFor(m => m.Status)
  @(Html.Kendo().DropDownListFor(m=> m.Status)
      .BindTo(Enum.GetNames(typeof(SearchStatusCriteria)).ToList())
      .HtmlAttributes(new {value = "All"}))
</div>

Model

    public enum SearchDateRanges
{
    [Description("Today")]
    Today = 0,

    [Description("Last 10 Days")]
    Last10Days = 1,

    /// <summary>
    /// The last 30 days.
    /// </summary>
    [Description("Last 30 Days")]
    Last30Days = 2,

    [Description("Last 60 Days")]
    Last60Days = 3,

    [Description("Last 90 Days")]
    Last90Days = 4,

    [Description("Custom Date Range")]
    CustomRange = 5
}

}

+5
source share
6 answers

, enum, description:

.BindTo(Enum.GetNames(typeof(SearchDateRanges)).ToList())

, . :

public static string GetEnumDescription(Enum value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

DescriptionAttribute[] attributes =
    (DescriptionAttribute[])fi.GetCustomAttributes(
    typeof(DescriptionAttribute),
    false);

if (attributes != null &&
    attributes.Length > 0)
    return attributes[0].Description;
else
    return value.ToString();
}

"", .

, .

+4

AFAIK, .

, Enum List<SelectListItem> :

public static List<SelectListItem> EnumToSelectList( Type enumType )
{
  return Enum
    .GetValues( enumType )
    .Cast<int>()
    .Select( i => new SelectListItem
      {
        Value = i.ToString(),
        Text = Enum.GetName( enumType, i ),
      }
    )
    .ToList()
}

DropDownList :

.BindTo( EnumToSelectList( typeof( SearchDateRanges ) ) )

, Description, , - , Reflection.

+5

NET MVC 5.1 @Html.EnumDropDownListFor.

.cshtml Views/EditorTemplate.

, cshtml

@(Html.Kendo().Grid<NameProved.Models.Issuer>()
.Name("IssuerGrid")
.Columns(columns =>
{
    columns.Bound(issuer => issuer.ID);
    columns.Bound(issuer => issuer.Name);
    columns.Bound(issuer => issuer.IssuerType);

    columns.Command(commands =>
        {
            commands.Edit();
            commands.Destroy();
        }).Title("Commands");
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable
    .Mode(GridEditMode.PopUp)
    )
.DataSource(datasource =>
    datasource
    .Ajax()
    .Events(events => events.Error("grid_error"))
    .Model(model =>
    {
        model.Id(issuer => issuer.ID);
        model.Field(issuer => issuer.ID).Editable(false).DefaultValue(0);
    })
    .Create(create => create.Action("Issuer_Create", "Admin"))
    .Read(read => read.Action("Issuer_Read", "Admin"))
    .Update(update => update.Action("Issuer_Update", "Admin"))
    .Destroy(destroy => destroy.Action("Issuer_Destory", "Admin"))
    )
.Pageable()

)
)

UIHint issuerType, enum.

Views/Shared/EditorTemplate/IssuerTypeEditor.cshtml,

@model NameProved.Models.IssuerType

@Html.EnumDropDownListFor(issuerType => issuerType)

UIHint.

public class Issuer
{

    public int ID { get; set; }
    public string Name { get; set; }

    [UIHint("IssuerTypeEditor")]
    public IssuerType IssuerType { get; set; }
}

Then you get it.

+3
source

I know that this thread is outdated, but in case someone goes on the same issue, here is another solution:

@(Html.Kendo().DropDownList()
    .Name("subscriptionTypeTest")
    .DataTextField("Text")
    .DataValueField("Value")
    .BindTo(Html.GetEnumSelectList(typeof(SubscriptionType)))
    .Deferred()
)
+1
source

You can try this

@(Html.Kendo().DropDownListFor(model => model.NoticePeriodType)
      .DataTextField("Text")
      .DataValueField("Value")
      .DataSource(source =>
         {
           source.Read(read =>
           {
              read.Action("LoadPeriodTypesAjax", "ControllerName"); //put controller name
            });
         })
   )


public JsonResult LoadPeriodTypesAjax()
{
    var values = Enum.GetValues(typeof(PeriodType)).Cast<int>(); //PeriodType= enum name
    var converter = TypeDescriptor.GetConverter(typeof(PeriodType));
    var datas = from value in values
                select new SelectListItem
                {
                    Text = converter.ConvertToString(value),
                    Value = value.ToValue(),
                };
    return Json(datas, JsonRequestBehavior.AllowGet);
}
0
source

Answer from Andre only works with ASP.Net MVC 5.1 and higher. In older versions you can useEnumHelper.GetSelectList

@(Html.Kendo().DropDownList()
    .Name("subscriptionTypeTest")
    .DataTextField("Text")
    .DataValueField("Value")
    .BindTo(EnumHelper.GetSelectList(typeof(SubscriptionType)))
    .Deferred()
)
0
source

All Articles