2012-11-16 52 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