2014-10-08 31 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