public IEnumerable<SelectListItem> GetList(int? ID)
 {
      return from s in db.List
             orderby s.Descript
             select new SelectListItem
             {
                 Text = s.Descript,
                 Value = s.ID.ToString(),
                 Selected = (s.ID == ID)
             };
 }


我将以上内容返回到视图并填充DropDownList。我想在上面的linq结果中添加默认的SelectListItem (0, "Please Select.."),然后将其返回到视图。这可能吗?

最佳答案

return new[] { new SelectListItem { Text = ... } }.Concat(
       from s in db.List
       orderby s.Descript
       select new SelectListItem
       {
           Text = s.Descript,
           Value = s.ID.ToString(),
           Selected = (s.ID == ID)
       });

关于asp.net-mvc - 添加默认的SelectListItem,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/894130/

10-10 22:09