我开始对此感到非常恼火,但无论如何,我都无法让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");
我这里没有文本/键值,因为文本/键也将是一个字符串和同样的东西。无论如何,通过此代码,它仍然没有选择"11:00 PM"作为默认项。
我该怎么做才能解决这个问题?
尝试如下:
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");