Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Listbox without ListItems

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

like image 508
DAK Avatar asked Aug 12 '26 08:08

DAK


1 Answers

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>())
like image 129
Andrei Avatar answered Aug 15 '26 04:08

Andrei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!