将项目插入@ Html.DropdownList

时间:2013-03-11 06:00:31

标签: asp.net-mvc razor

这是我目前用于简单选择的剃刀代码。

@Html.DropDownList("Season", Model.Seasons.Select(s => new SelectListItem { Text = s.Name, Value = s.SeasonId.ToString() }), new { @class = "sel_season" })

我想要做的是添加/插入一个名为“全部”的附加选项。 我试过Concat,但我似乎无法让它工作。

@Html.DropDownList("Season", Model.Seasons.Select(s => new SelectListItem { Text = s.Name, Value = s.SeasonId.ToString() }).Concat( new {"All", "0"}), new { @class = "sel_season" })

2 个答案:

答案 0 :(得分:2)

以下示例使用HtmlHelper class中的重载扩展方法:

@Html.DropDownList("Season"
    , Model.Seasons.Select(s => new SelectListItem {
        Text = s.Name,
        Value = s.SeasonId.ToString()
    })
    , "All"
    , new { @class = "sel_season" }
)

答案 1 :(得分:0)

试试这个

@Html.DropDownList("Season", 
                  Model.Seasons
                    .Select(s =>
                       new SelectListItem { Text = s.Name, Value = s.SeasonId.ToString() })
                    .ToList().Insert(0, SelectListItem { Text = "All",Value =  "0"}), 
                   new { @class = "sel_season" })