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.我想在返回到视图之前添加一个默认的SelectListItem(0,“请选择..”)到上述的linq结果.这可能吗?
解决方法
return new[] { new SelectListItem { Text = ... } }.Concat( from s in db.List orderby s.Descript select new SelectListItem { Text = s.Descript,Selected = (s.ID == ID) });