没有类型的ViewData项

时间:2013-09-04 09:58:00

标签: c# asp.net vb.net

早上好,我是语言vb.net和asp.net的初学者,我得到了这个异常,我想知道我应该在哪里定义这个ViewData项......

例外:

There is no ViewData item of type "IEnumerable <SelectListItem>" with key "typeId."

异常引用的代码行:

<label>Type: @Html.DropDownList("typeId", DirectCast(ViewBag.Type, IEnumerable(Of SelectListItem)), New With { key.id = "typeId" })</label>

1 个答案:

答案 0 :(得分:0)

在控制器当然,在调用视图之前。 在这里,您有ViewBag.Type,但似乎您从未定义过此属性。

在控制器中调用View之前,添加一行设置ViewBag.Type

类似的东西:

ViewBag.Type = new List<SelectListItem>() ...

抱歉,这是C#,但您肯定能够将其转换为VB(我不记得具体的工作原理)

编辑:

VB版:

ViewBag.Type = New List(Of SelectListItem)()

感谢@DominicKexel

相关问题