2013-01-04 82 views
1

我有一個關於@ Html.DropDownListFor如何使用Html.DropDownListFor在另一個

問我有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 

的視圖是爲了創建工作記錄 所以我想說明視圖可以chooise會員名稱和寫工作備忘錄

所以在控制器

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 

並在視圖

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

我想選擇的名字,但它不工作...

錯誤消息:

否類型是「IEnumerable的」和索引「數字」 ViewData項目

我不知道什麼是錯,請幫助我。

回答

2

從提供的示例中看來,你可能會ViewData("List")

被混合起來ViewData("Item")那麼試試這個,而不是

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

非常感謝!沒關係! –

相關問題