我开始对此感到非常恼火,但无论怎样,我都无法让SelectedValue
为SelectList
工作。
这是我的代码:
List<string> PickupTimeList = EntityLogic.PopulateTimeSlots(vm.DateMileage.PickupDate,
vm.DateMileage.PickupDate.AddHours(-2), vm.DateMileage.PickupDate.AddHours(2).AddMinutes(15));
ViewBag.PickupTimeList = new SelectList(PickupTimeList, "11:00 PM");
我这里没有文本/键值,因为Text / Key也是一个字符串和相同的东西。无论如何,通过这段代码,它仍然没有选择“11:00 PM”作为默认项目。
我该怎么做才能解决这个问题?
答案 0 :(得分:0)
试试这样:
var pickupTimeList = EntityLogic.PopulateTimeSlots(
vm.DateMileage.PickupDate,
vm.DateMileage.PickupDate.AddHours(-2),
vm.DateMileage.PickupDate.AddHours(2).AddMinutes(15)
)
.Select(x => new SelectListItem
{
Value = x,
Text = x,
});
ViewBag.PickupTimeList = new SelectList(PickupTimeList, "Value", "Text", "11:00 PM");