I am trying to add an empty ListBox to my Razor view page to store run time values. But It gives me an error message saying There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'myListBox'. I am aware that there is such viewdata. But, when I do the same with textbox, it works.
@Html.TextBox("myTextBox")//works alright, displays a textbox
@Html.ListBox("myListBox") // gives error
How to display an empty listbox and why the above method works for textbox but for listbox?
Just found a workaround to create an empty ListBox by passing an empty
SelectListbut still wondering why the original one did not work.@Html.ListBox("myListBox", new SelectList(new[] {""}));