mam enum
klasa i wiążę jej wartości Kendo DropDownListFor
w trybie tworzenia bez problemu. W trybie edycji te wartości są powiązane Kendo DropDownListFor
również, ale indeksu bieżącej wartości nie wybrano. Mam na myśli, że rekord IdentityType
jest Passport
, ale DropDownList
pokazuje "Wybierz" jak w trybie tworzenia. Jak mogę to naprawić?
Uwaga: Podczas używania @Html.DropDownListFor
zamiast Kendo().DropDownListFor
to działa, ale chcę to zrobić za pomocą Kendo().DropDownListFor
.
Enum (IdentityType):
public enum IdentityType
{
[Description("Identity Card")]
IdentityCard= 1,
[Description("Driver License")]
DriverLicense= 2,
[Description("Passport ")]
Passport = 3
}
Metoda Enum Helper:
/// <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();
}
Widok:
@(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" }))
Odpowiedzi:
1 dla odpowiedzi № 1Zmierzyłem ten sam problem, a jedynym rozwiązaniem, jakie znalazłem, było ustawienie wartości ręcznej:
@(Html.Kendo().DropDownListFor(m => m.IdentityType)
...
.Value(Model.IdentityType.ToString())
)