2014-10-08 49 views
0

我似乎無法讓我的HTML.DropDownListFor()與我的結構正常工作。DropDownListFor使用類別列表

CategoriesEditViewModel

Public Class CategoriesEditViewModel 
    Public Property Categories As List(Of cihCategoryOrgDef) 
    Public Property Category As cihCategoryOrgDef 
    Public Property lists As cihLists = New cihLists() 

    Public Property SelectedItem As String 

    Public Sub New(catId as Guid) 
    SelectedItem = codId.ToString 
    lists.loadClubWaivers(ZenCommon.CurrentOrgId) 
    End Sub 
End Class 

cihLists

Private orgWaiverList As List(of cihWaiver) 

    Public Sub loadClubWaivers(orgId as Guid) 
    'Go to database and populate orgWaiverList 
    End Sub 

Public ReadOnly Property organizationClubWaivers() As List(Of cihWaiver) 
     Get 
      Return orgWaiverList 
     End Get 
    End Property 

cihWaiver

Public Class cihWaiver 
    Public waiverId As Guid = Guid.Empty 
    Public waiverName As String = "" 
    Public waiverText As String = "" 
End Class 

編輯視圖頁

@Html.DropDownListFor(Function(m) m.SelectedItem, New SelectList(Model.lists.organizationClubWaivers, "waiverId", "waiverText", Model.lists.organizationClubWaivers)) 

我得到的錯誤是'Library.cihWaiver' does not contain a property with the name 'waiverId'.

但cihWaiver類顯然有一個項目爲 'waiverId'。我有一段時間沒有做過MVC的東西,所以也許我會說這一切都是錯誤的。

回答

0

答案是爲改變我的cihWaiver類此爲簡單:爲什麼關鍵字Property是如此重要

Public Class cihWaiver 
    Public Property waiverId As Guid = Guid.Empty 
    Public Property waiverName As String = "" 
    Public waiverText As String = "" 
End Class 

甘蔗有人向我解釋?

+1

由於屬性是更好的代碼設計,應該用於代替公共領域。不使用字段的原因是因爲獲取/設置值時發生的反射。它只是尋找屬性而不是領域。 – TyCobb 2014-10-08 17:37:14

+0

由於@TyCobb寫了一個屬性是**不同於**字段。它不僅僅是語法。以下是更多信息:http://blogs.msdn.com/b/vbteam/archive/2009/09/04/properties-vs-fields-why-does-it-matter-jonathan-aneja.aspx – 2014-10-08 20:54:07