vb.net 如何在另一个中使用 Html.DropDownListFor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14152055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:46:14  来源:igfitidea点击:

How to use Html.DropDownListFor in another

vb.netasp.net-mvc-4

提问by Echo Kao

I have a Question about @Html.DropDownListFor

我有一个关于@Html.DropDownListFor 的问题

I have 2 class :

我有 2 个班级:

Public Class A01WorkRecord
Public Property counter As Int32
Public Property MembetNumber As Int16
Public Property Memo As String
Public Property WorkDate As Date
End Class

Public Class B01Member
Public Property counter As Int32
Public Property Number As Int16
Public Property Name As Date
End Class

the VIEW is for create Work Record so I want the view can chooise Member Name and write work memo

VIEW 用于创建工作记录,所以我希望视图可以选择成员名称并编写工作备忘录

so in the Controller

所以在控制器中

Function Index() As ActionResult
    Dim q = From p In db.B01s
            Select p.Number, p.Name

    Dim ListItem As List(Of SelectListItem) = New List(Of SelectListItem)
    For Each l In q
        ListItem.Add(New SelectListItem With {.Text = l.Name, .Value = l.Number})
    Next
    ViewData("List") = New SelectList(ListItem, "Value", "Text", "")

    Return View()
End Function

and in the View

并在视图中

@Html.DropDownListFor(Function(model) model.MembetNumber ,ViewData("Item"))

I want to choose name, but it's not working ...

我想选择名字,但它不起作用......

the error message:

错误信息:

No Type is 'IEnumerable' and index 'Number' ViewData Item

无类型为 'IEnumerable' 和索引为 'Number' ViewData 项

I don't Know what's Wrong, please help me.

我不知道怎么了,请帮助我。

回答by Kane

From the example provided it appears that you are might be mixing up ViewData("Item")with ViewData("List")

从提供的示例看来,您可能正在ViewData("Item")ViewData("List")

So try this instead

所以试试这个

@Html.DropDownListFor(Function(model) model.MembetNumber, ViewData("List"))