Louise
Louise

Reputation: 157

How to get the first item in a SelectList to be empty in ASP.NET MVC project?

I have looked online for a solution, but the solutions I have found are applicable to code that has been implemented quite differently to mine.

My dropdownlist is populated with a List called SelectionList, this is populated in my controller.

        SelectionList= (db.Items.ToList())

The view is as followed:

                @Html.Label("Selection:", htmlAttributes: ...)
                <div class="col-md-3">
                    @Html.DropDownListFor(m => m.Table.SelectionID, new SelectList(Model.SelectionList, "SelectionID", "ItemName"), new { @class = ... })
                </div>

There are no issues with displaying the selectionlist, however I would like the first item to be blank so that the dropdownlist initially does not display any items.

Any help would be greatly appreciated. :)

Upvotes: 0

Views: 1062

Answers (1)

Brad C
Brad C

Reputation: 2982

Try this:

@Html.DropDownListFor(m => m.Table.SelectionID, new SelectList(Model.SelectionList, "SelectionID", "ItemName"), "", new { @class = ... })

That third param with the empty string specifies the option label.

Upvotes: 1

Related Questions