我有一个返回列表项的方法。当我尝试用这些项目填充 DropDownList 时,就会出现问题。我每次都得到一个空异常。问题在哪里?
方法:
public IEnumerable<SelectListItem> GetItems()
{
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem { Text = "Ptuj", Value = "1" });
list.Add(new SelectListItem { Text = "Celje", Value = "2" });
list.Add(new SelectListItem { Text = "Laško", Value = "3" });
list.Add(new SelectListItem { Text = "Maribor", Value = "4" });
list.Add(new SelectListItem { Text = "Ljubljana", Value = "5" });
list.Add(new SelectListItem { Text = "N/A", Value = "6" });
foreach (SelectListItem item in list)
{
yield return item;
}
}
看法:
@Html.LabelFor(model => model.PlaceOfBirth)
@Html.DropDownListFor(model => model.PlaceOfBirth, //Exception thrown in this line
Model.GetItems())