【发布时间】:2016-04-03 02:25:07
【问题描述】:
我有一个enum 类,我在创建模式下将其值绑定到Kendo DropDownListFor 没有任何问题。在编辑模式下,这些值也绑定到Kendo DropDownListFor,但未选择当前值的索引。我的意思是记录的IdentityType 是Passport,但DropDownList 在创建模式下显示“请选择”。我该如何解决?
注意:当使用@Html.DropDownListFor 而不是Kendo().DropDownListFor 时它可以工作,但我想通过使用Kendo().DropDownListFor 来执行此操作。
枚举(IdentityType):
public enum IdentityType
{
[Description("Identity Card")]
IdentityCard= 1,
[Description("Driver License")]
DriverLicense= 2,
[Description("Passport ")]
Passport = 3
}
枚举辅助方法:
/// <summary>
/// For displaying enum descriptions instead of enum names on grid, ddl, etc.
/// </summary>
public static string GetDescription<T>(this T enumerationValue)
where T : struct
{
Type type = enumerationValue.GetType();
if (!type.IsEnum)
{
throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue");
}
//Tries to find a DescriptionAttribute for a potential friendly name
//for the enum
MemberInfo[] memberInfo = type.GetMember(enumerationValue.ToString());
if (memberInfo != null && memberInfo.Length > 0)
{
object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
//Pull out the description value
return ((DescriptionAttribute)attrs[0]).Description;
}
}
//If we have no description attribute, just return the ToString of the enum
return enumerationValue.ToString();
}
查看:
@(Html.Kendo().DropDownListFor(m => m.IdentityType)
.HtmlAttributes(new { @class = "k-dropdown" })
.OptionLabel("Please select").BindTo(Enum.GetValues(
typeof(Enums.IdentityType)).Cast<Enums.IdentityType>()
.Select(x => new SelectListItem { Text = x.GetDescription(), Value = x.ToString() }))
)
//This works but I want to perform this by uisng Kendo().DropDownListFor:
@Html.DropDownListFor(x => x.IdentityType,
new SelectList(Enum.GetNames(typeof(Enums.IdentityType)), new { @class = "k-dropdown" }))
【问题讨论】:
标签: asp.net-mvc enums kendo-ui kendo-asp.net-mvc kendo-dropdown