我不确定我做错了什么。我想返回要在下拉框中使用的值/项对的列表。我收到以下错误。
Cannot implicitly convert type
System.Collections.Generic.List<System.Web.Mvc.SelectListItem> to
System.Collections.Generic.List<string>
导致错误的方法如下。
方式
//Get the shift list based on StationId which is foreign key in Shifts table.
public List<string> GetShiftsByStationId(int stationId)
{
//Created DataContext to query DB.
using (DataManager db = new DataManager())
{
//returns all the records from table based on StationId in list format.
return db.Shifts.Where(query => query.StationId == stationId).Select(q => new SelectListItem { Value = q.ShiftId.ToString(), Text = q.Code }).ToList();
}
}
答案 0 :(得分:5)
public List<string> GetShiftsByStationId(int stationId)
应如下所示(因为在您的LINQ查询中,您选择的是new SelectListItem
)
public List<SelectListItem> GetShiftsByStationId(int stationId)