iGanja
iGanja

Reputation: 2464

inserting an item to a @Html.DropdownList

This is my current razor code for a simple select.

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

What I want to do is add/insert an additional option item called "All". I have tried Concat, but I can't seem to get it to work.

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

Upvotes: 0

Views: 1683

Answers (2)

K D
K D

Reputation: 5989

Try This

@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" })

Upvotes: 0

Alex
Alex

Reputation: 35409

The following sample uses an overloaded extension method in the HtmlHelper class:

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

Upvotes: 2

Related Questions