Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an empty dropdown list with default value?

I want to create an empty dropdown list that has just default value.I use the following code:

@Html.DropDownList("parent","--Select Parent--")

But in running time I see this error:

There is no ViewData item of type 'IEnumerable' that has the key 'parent'.

How can I solve it? Thanks.

like image 967
Hamid Reza Avatar asked Mar 26 '13 14:03

Hamid Reza


1 Answers

You can build an empty DropDownList like this:

@Html.DropDownList("parent", Enumerable.Empty<SelectListItem>(), "--Select Parent--")

Reference: Build an empty MVC DropdownListFor for a Cascade Sub-List

like image 116
stenlytw Avatar answered Sep 28 '22 01:09

stenlytw