ASP.NET MVC显示外键值

ASP.NET MVC显示外键值,asp.net,entity-framework,foreign-keys,one-to-many,Asp.net,Entity Framework,Foreign Keys,One To Many,我有一个带有以下两个实体类的电话簿应用程序: 连络 public class Contact { [Key] public int id { get; set; } public String fName { get; set; } public String lName { get; set; } public String phoneNumber { get; set; } public PhoneType phoneType { get; s

我有一个带有以下两个实体类的电话簿应用程序:

连络

public class Contact
{
    [Key]
    public int id { get; set; }
    public String fName { get; set; }
    public String lName { get; set; }
    public String phoneNumber { get; set; }
    public PhoneType phoneType { get; set; }

}

PhoneType.cs

public class PhoneType
{
    [Key]
    public int id { get; set; }
    public String phoneType { get; set; }

    public ICollection<Contact> Contacts { get; set; } = new List<Contact>();
}
公共类电话类型
{
[关键]
公共int id{get;set;}
公共字符串phoneType{get;set;}
公共ICollection联系人{get;set;}=new List();
}
我的索引页显示联系人姓名和电话号码,但我也希望它显示电话类型

我尝试了不同的方法,但我的电话类型是空的

现在,在我的索引视图中,我有以下代码:

<td>
@Html.DisplayFor(modelItem => item.phoneType.phoneType)
</td>

@DisplayFor(modelItem=>item.phoneType.phoneType)
但是电话类型显示为空


有什么想法吗

如果您是从实体框架获取它们,则可能不包括其他类:

var contacts = _context.Contact.Include(p => p.phoneType).ToList();

我试着把这段代码放在我的控制器中,但它仍然不起作用。我看了你的其他问题,所以如果你完全复制我的代码,那么它就不会起作用,但也许这会-var contacts=context.contacts.Include(p=>p.phoneType.ToList();您可能还需要使用类似var contacts=context.contacts.Include(“phonetype”).ToList()的字符串来指定它;