problem with Html.DropDownFor and selected item
I can't figure Html.DropDownFor out to work properly when SelectListItem Text is different from Value. This issue is when marking an specific item as selected="true" and everything is failed! (Nothing is marked as selected="true")
public IEnumerable<SelectListItem> HaveFail {
get {
return
IoC.Container.Resolve<Something>().GetAll().Select(
x =>
new SelectListItem {
Text = x.Key,
开发者_开发百科 Value = x.Value.ToString(),
Selected = (string.Compare(x.Key, myValue) == 0)
});
}
}
public IEnumerable<SelectListItem> WorksFine {
get {
return
IoC.Container.Resolve<Something>().GetAll().Select(
x =>
new SelectListItem {
Text = x.Key,
Value = x.Key,
Selected = (string.Compare(x.Key, myValue) == 0)
});
}
}
You've got it backwards.
new SelectListItem
{
Text = x.Value.ToString(),
Value = x.Key,
Selected = (string.Compare(x.Key, myValue) == 0)
}
精彩评论