Kendo dropdown binding

2019-09-16 04:57发布

问题:

I have this enum:

public enum PayTerms
    {       
        [Display(Name = "30")]
        _30 = 1,        
        [Display(Name = "60")]
        _60,        
        [Display(Name = "90")]
        _90,        
        [Display(Name = "120")]
        _120,        
        CAD
    }

Using this template I manage to create dropdown list with proper names:

@model PayTerms?

<div class="k-edit-label">@Html.LabelFor(x => x)</div>
<div class="k-edit-field">
    @(Html.Kendo().DropDownListFor(m => m)
        .BindTo(EnumHelper.GetSelectList(typeof(PayTerms)))
        .OptionLabel("-- Select --"))
</div>

But I have problems with binding. Currently for each value for my enum property selected value in dropdown is "--Select--" How can I bind selected value for the dropdown to enum value?

UPDATE:

Also I have tried EnumHelper.GetSelectList(typeof(Erp.Shared.Contract.PayTerms), Model.Value) but also have no luck

回答1:

Kendo MVC helper has a problem with enums as it can't figure out whether to bind to the integer representation of the enum or the string representation. The default MVC dropdown list has no such problem.

http://www.telerik.com/forums/problem-binding-enum-to-dropdownlist#ZabuB0_2A0OserEwBh_etQ

Try adding an explicit .Value() to the definition:

@(Html.Kendo().DropDownListFor(m => m)
    .BindTo(EnumHelper.GetSelectList(typeof(PayTerms)))
    .Value(((int) Model).ToString())
    .OptionLabel("-- Select --"))