C# 在视图中显示ICollection
我有一个从我的SQL数据库自动生成的模型C# 在视图中显示ICollection,c#,.net,asp.net-mvc,razor,C#,.net,Asp.net Mvc,Razor,我有一个从我的SQL数据库自动生成的模型 class Organization { public Organization() { this.ContactTitles = new HashSet<ContactTitle>(); this.OrganizationAddresses = new HashSet<OrganizationAddress>(); this.OrganizationBusiness
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; }
}
班级组织
{
公共组织()
{
this.ContactTitles=new HashSet();
this.OrganizationAddresses=新哈希集();
this.OrganizationBusinessTypes=new HashSet();
this.OrganizationCountries=new HashSet();
this.OrganizationEmails=new HashSet();
this.OrganizationMemberships=new HashSet();
this.OrganizationNotes=new HashSet();
this.OrganizationPhones=new HashSet();
this.OrganizationWebsites=new HashSet();
this.Contacts=newhashset();
this.OrganizationIndustryCodes=新哈希集();
}
public int OrganizationID{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection联系人标题{get;set;}
公共虚拟ICollection组织地址{get;set;}
公共虚拟ICollection OrganizationBusinessTypes{get;set;}
公共虚拟ICollection组织国家{get;set;}
公共虚拟ICollection组织电子邮件{get;set;}
公共虚拟ICollection组织成员身份{get;set;}
公共虚拟ICollection组织注释{get;set;}
公共虚拟ICollection组织电话{get;set;}
公共虚拟ICollection组织网站{get;set;}
公共虚拟ICollection联系人{get;set;}
公共虚拟ICollection组织行业代码{get;set;}
}
在“我的组织”视图中的“我的索引”页上,它与我的组织模型是强类型的
我试图在组织索引页面上显示成员信息,我认为应该在ICollection中。除非我无法解释它的作用
当我要放置一个
@Html.DisplayFor(modelItem=>item.OrganizationMemberships.
若要获取OrganizationMemberships表中的数据,它不会显示在IntelliSense上。我只需要能够显示数据,不必使用表单提交任何更改。因为模型是可枚举类型--@model PagedList.IPagedList
--您需要迭代在主视图中浏览它们:
@foreach (var organization in Model)
{
@Html.DisplayFor(model => organization)
}
接下来,为类组织
创建一个视图。在视图/共享/显示模板
下添加一个视图组织.cshtml
:
@model VAGTC.Models.Organization
现在,这是呈现类的主视图。在这里,您可以迭代成员项:
@foreach (var membership in Model.OrganizationMemberships)
{
@Html.DisplayFor(model => membership)
}
现在,再次为
OrganizationMembership
类创建一个局部视图,方法是在Views/Shared/DisplayTemplates
下添加OrganizationMembership.cshtml
,我有@model PagedList.IPagedList
来利用PagedList的内容。这就是为什么我不能在我的网页上处理OrganizationMembershipOrganization View?Model.OrganizationMembership也不起作用-它声明它不包含定义。@kifi在视图顶部声明的模型类型是什么?@Model PagedList.IPagedList
这在我的View@kifi我根据这些信息修改了我的答案——请看上面。关键是学习关于使用部分视图显示模板——如果您有一个类类型的显示模板部分视图,那么MVC在找到该类类型的DisplayFor
时会自动使用该部分视图。太棒了!非常感谢!