SelectList未从List <string> </string>中选择默认值

时间:2014-02-13 22:30:13

标签: c# asp.net-mvc list selectlist

我开始对此感到非常恼火,但无论怎样,我都无法让SelectedValueSelectList工作。

这是我的代码:

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”作为默认项目。

我该怎么做才能解决这个问题?

1 个答案:

答案 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");