我需要一个下拉列表来填充一个简单的数字列表。我不确定是否可以在不必创建新的 SelectList 的情况下做到这一点,因为它只是数字。
@Html.DropDownListFor(model => model.NumberPassengers, new SelectList(Model.NumberPassengers))
真的不知道在我的模型中做什么:
public int[] Passengers = { 1, 2, 3, 4, 5, 6 };
我需要一个下拉列表来填充一个简单的数字列表。我不确定是否可以在不必创建新的 SelectList 的情况下做到这一点,因为它只是数字。
@Html.DropDownListFor(model => model.NumberPassengers, new SelectList(Model.NumberPassengers))
真的不知道在我的模型中做什么:
public int[] Passengers = { 1, 2, 3, 4, 5, 6 };
您需要创建一个新的列表SelectListItem
。然后我通常将它放在 ViewBag 中,因为你不希望它回来。然后将其绑定到您的DropDownListFor
控件,这是我做过多次的:
private IEnumerable<SelectListItem> PopulateTimes()
{
List<SelectListItem> timeList = new List<SelectListItem>();
for (int count = 0; count < 24; count++)
{
timeList.Add(new SelectListItem()
{
Value = count.ToString("00"),
Text = count.ToString("00") + ":00",
Selected = count == 9,
});
}
return timeList;
}
然后
ViewBag.DepartureTimes = PopulateTimes();
然后
@Html.DropDownListFor(model => model.DepartureTime, (IEnumerable<SelectListItem>)ViewBag.DepartureTimes)