asp.net-mvc - 添加默认的 SelectListItem

标签 asp.net-mvc linq

 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)
             };
 }

我将上述返回到 View 并填充 DropDownList .我想添加一个默认值 SelectListItem (0, "Please Select..")在返回到 View 之前到上面的 linq 结果。这可能吗?

最佳答案

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/

相关文章:

c# - 在 MVC 4 ASP.NET 中处理 'Many to One' 关系

c# - 使用 LINQ to SQL 的奇怪排序规则问题

c# - WPF C# LINQ : Operator '&&' cannot be applied to operands of type 'string' and 'string' query

c# - 使用 Linq 读取第 n 层列表并将所需数据保存到另一个列表

c# - 非泛型类型 'ActionResult' 不能与类型参数一起使用

c# - MVC3 + 如何获取当前登录用户的用户名

asp.net-mvc - 如何更改 .NET Core 中 SQL 数据库中的 FirstName 属性?

asp.net-mvc - Crystal 报表与 Blazor

.net - IActionFilter 与 IResultFilter

c# - 将强类型表达式转换为匿名类型返回