当SelectListItem Text与Value不同时,我无法使Html.DropDownFor正常工作。此问题是将特定项目标记为selected =“true”且一切都失败了! (没有标记为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)
});
}
}
答案 0 :(得分:2)
你已经倒退了。
new SelectListItem
{
Text = x.Value.ToString(),
Value = x.Key,
Selected = (string.Compare(x.Key, myValue) == 0)
}