在剃刀视图中添加一个空的列表框 - @ Html.ListBox("myLIstBox")抛出错误

Bis*_*del 2 asp.net-mvc listbox razor asp.net-mvc-3

我正在尝试将一个空的ListBox添加到我的Razor视图页面来存储运行时值.但它给了我一个错误信息说There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'myListBox'.我知道有这样的viewdata.但是,当我使用文本框执行相同操作时,它可以正常工作.

  @Html.TextBox("myTextBox")//works alright, displays a textbox 
  @Html.ListBox("myListBox") // gives error
Run Code Online (Sandbox Code Playgroud)

如何显示一个空的列表框以及为什么上述方法适用于文本框而不是列表框?

Bis*_*del 5

刚刚找到一个解决方法来创建一个空的ListBox,通过传递一个空,SelectList但仍然想知道为什么原来的一个不起作用.

@Html.ListBox("myListBox", new SelectList(new[] {""}));