2012-11-16 50 views
1

我有一個從我的SQL數據庫中自動生成的模型。在視圖中顯示ICollection

class Organization 
{ 
    public Organization() 
    { 
     this.ContactTitles = new HashSet<ContactTitle>(); 
     this.OrganizationAddresses = new HashSet<OrganizationAddress>(); 
     this.OrganizationBusinessTypes = new HashSet<OrganizationBusinessType>(); 
     this.OrganizationCountries = new HashSet<OrganizationCountry>(); 
     this.OrganizationEmails = new HashSet<OrganizationEmail>(); 
     this.OrganizationMemberships = new HashSet<OrganizationMembership>(); 
     this.OrganizationNotes = new HashSet<OrganizationNote>(); 
     this.OrganizationPhones = new HashSet<OrganizationPhone>(); 
     this.OrganizationWebsites = new HashSet<OrganizationWebsite>(); 
     this.Contacts = new HashSet<Contact>(); 
     this.OrganizationIndustryCodes = new HashSet<OrganizationIndustryCode>(); 
    } 

    public int OrganizationID { get; set; } 
    public string Name { get; set; } 

    public virtual ICollection<ContactTitle> ContactTitles { get; set; } 
    public virtual ICollection<OrganizationAddress> OrganizationAddresses { get; set; } 
    public virtual ICollection<OrganizationBusinessType> OrganizationBusinessTypes { get; set; } 
    public virtual ICollection<OrganizationCountry> OrganizationCountries { get; set; } 
    public virtual ICollection<OrganizationEmail> OrganizationEmails { get; set; } 
    public virtual ICollection<OrganizationMembership> OrganizationMemberships { get; set; } 
    public virtual ICollection<OrganizationNote> OrganizationNotes { get; set; } 
    public virtual ICollection<OrganizationPhone> OrganizationPhones { get; set; } 
    public virtual ICollection<OrganizationWebsite> OrganizationWebsites { get; set; } 
    public virtual ICollection<Contact> Contacts { get; set; } 
    public virtual ICollection<OrganizationIndustryCode> OrganizationIndustryCodes { get; set; } 
} 

在我的組織視圖中,在我的索引頁上 - 強制鍵入我的組織模型。

我想在組織索引頁上顯示我認爲應該在ICollection中的成員資格信息。除非我錯過了解釋它的作用。

當我將@Html.DisplayFor(modelItem => item.OrganizationMemberships.抓取OrganizationMembership表中的數據時,它不會顯示在IntelliSense上。我只需要能夠顯示數據,我不必提交任何形式的更改。

回答

3

由於模型是一個枚舉類型 - @model PagedList.IPagedList<VAGTC.Models.Organization> - 你需要通過他們在你的主要觀點進行迭代:

@foreach (var organization in Model) 
{ 
    @Html.DisplayFor(model => organization) 
} 

下,爲該類創建一個Organizationdisplay template。在Views/Shared/DisplayTemplates添加視圖Organization.cshtml

@model VAGTC.Models.Organization 

現在這是使你的類主視圖。現在再次

@foreach (var membership in Model.OrganizationMemberships) 
{ 
    @Html.DisplayFor(model => membership) 
} 

,通過Views/Shared/DisplayTemplates下加入OrganizationMembership.cshtml創造了OrganizationMembership類的局部視圖:在這裏,你可以遍歷會員項目。

+0

我有'@model PagedList.IPagedList '來利用PagedList的東西。所以這就是爲什麼我在組織視圖上無法使用OrganizationMemberships做任何事情? Model.OrganizationMembership也不起作用 - 它聲明它不包含定義。 – cfisher

+0

@kifi什麼是你的視圖頂部聲明的模型類型? – McGarnagle

+0

'@model PagedList.IPagedList ' 這是在我的視圖的頂部聲明 – cfisher