MVC Listbox without ListItems

DAK picture DAK · Jun 17, 2013 · Viewed 10.2k times · Source

I'm trying to show 2 listboxes one with all the possible choices & other will populate as users select their choices but for some reason I can't add an empty listbox.

Here is the code:

@Html.ListBox("somename")

& my error is: There is no ViewData item of type 'IEnumerable' that has the key 'somename'.

It I replace the above line of code with the following then it works fine but I want an empty listbox:

@Html.ListBox("somename", liMyList)

where liMyList is SelectListItem.

is it possible to have an empty listbox in MVC? Please help

Answer

Andrei picture Andrei · Jun 17, 2013

One option is just a pure HTML:

<select name="somename" multiple="multiple">
</select>

Another is to supply an empty list of items:

@Html.ListBox("somename", new List<SelectListItem>())