C# 未传递到视图的列表项

C# 未传递到视图的列表项,c#,asp.net-mvc,visual-studio,entity-framework,.net-core,C#,Asp.net Mvc,Visual Studio,Entity Framework,.net Core,对于我的小型web应用程序项目,我创建了一个名为Company的模型,其中包含了公司的基本信息以及来自不同业务合作伙伴的销售代表列表。这是我的联系方式: public class Company { public int ID { get; set; } public string Name { get; set; } public string Promo { get; set; } // Yes or No field

对于我的小型web应用程序项目,我创建了一个名为Company的模型,其中包含了公司的基本信息以及来自不同业务合作伙伴的销售代表列表。这是我的联系方式:

 public class Company
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Promo { get; set; } // Yes or No field
        public List<Contact> Contacts { get; set; }
        public class Contact
        {
            [Key]
            public int ContactID { get; set; }
            public int CompanyID { get; set; }
            public string ContactName { get; set; }
            public string ContactNumber { get; set; }
        }
    }
上市公司
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串Promo{get;set;}//是或否字段
公共列表联系人{get;set;}
公共类联系人
{
[关键]
public int ContactID{get;set;}
public int CompanyID{get;set;}
公共字符串ContactName{get;set;}
公共字符串ContactNumber{get;set;}
}
}
这是我将数据传递到本地数据库的方式:

var companiesData = new Company[]
        {
            new Company
            {
             Name = "Some name", Promo="Y", Contacts = new List<Contact> { new Contact {ContactName="John Doe", ContactNumber="(828)292-2912", CompanyID=1}, 
           }}, // ... some more contacts
        };
        foreach (Company c in companiesData)
        {
            context.Companies.Add(c);
        }
        context.SaveChanges();
var companiesData=新公司[]
{
新公司
{
Name=“Some Name”,Promo=“Y”,Contacts=new List{new Contact{ContactName=“John Doe”,ContactNumber=“(828)292-2912”,CompanyID=1},
}},//…更多联系人
};
foreach(公司数据中的公司c)
{
上下文。公司。添加(c);
}
SaveChanges();
如何将联系人列表项加载到razor视图中?我正在查看我的数据库,其中显示了一个空白的“联系人”字段。其余的数据显示得很好

实体:

public class Company
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Promo { get; set; } 
    public virtual List<Contact> Contacts { get; set; }       
}

public class Contact
{
    [Key]
    public int ContactID { get; set; }
    [ForeignKey("Company")]
    public int CompanyID { get; set; }
    public virtual Company Company { get; set; }
    public string ContactName { get; set; }
    public string ContactNumber { get; set; }
}
上市公司
{
[关键]
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串Promo{get;set;}
公共虚拟列表联系人{get;set;}
}
公共类联系人
{
[关键]
public int ContactID{get;set;}
[外键(“公司”)]
public int CompanyID{get;set;}
公共虚拟公司公司{get;set;}
公共字符串ContactName{get;set;}
公共字符串ContactNumber{get;set;}
}
测试线束:

public static void Main()
{
    Console.WriteLine("Hello World");
    FakeDbContext context = new FakeDbContext();
    var companiesData = new Company[]
    {
        new Company
        {
            Name = "Some name", Promo="Y", Contacts = new List<Contact> { new Contact {ContactName="John Doe", ContactNumber="(828)292-2912", CompanyID=1}},
        },
        new Company
        {
            Name = "Another name", Promo="N",  Contacts = new List<Contact> { new Contact {ContactName="Jane Doe", ContactNumber="(828)292-2912", CompanyID=2}},
        },
    };
    foreach (Company c in companiesData)
    {
        context.Companies.Add(c);
        foreach (Contact contact in c.Contacts)
        {
            context.Contacts.Add(contact);  
        }
    }
    context.SaveChanges();
    Console.WriteLine("Save done.");
    var companies = context.Companies.ToList();
    Console.WriteLine("companies.Count: " + companies.Count);
    for (int i = 0; i < companies.Count; i++)
    {
        Console.WriteLine(string.Format("company[{0}].Name: {1}", i,     companies[i].Name));
        for (int j = 0; j < companies[i].Contacts.Count; j++)
        {               
Console.WriteLine(string.Format("company[{0}].Contacts[{1}].ContactName: {2}", i, j, companies[i].Contacts[j].ContactName));
        }
    }
    Console.WriteLine("Bye World");
}
publicstaticvoidmain()
{
Console.WriteLine(“你好世界”);
FakeDbContext=new FakeDbContext();
var CompanyData=新公司[]
{
新公司
{
Name=“Some Name”,Promo=“Y”,Contacts=new List{new Contact{ContactName=“John Doe”,ContactNumber=“(828)292-2912”,CompanyID=1},
},
新公司
{
Name=“另一个名字”,Promo=“N”,Contacts=new List{new Contact{ContactName=“Jane Doe”,ContactNumber=“(828)292-2912”,CompanyID=2}},
},
};
foreach(公司数据中的公司c)
{
上下文。公司。添加(c);
foreach(c.触点中的触点触点)
{
context.Contacts.Add(contact);
}
}
SaveChanges();
Console.WriteLine(“保存完成”);
var companys=context.companys.ToList();
Console.WriteLine(“companys.Count:+companys.Count”);
对于(int i=0;i
输出:

你好,世界

保存完成

公司。计数:2

公司[0]。名称:某个名称

公司[0]。联系人[0]。联系人姓名:John Doe

公司[1]。名称:其他名称

公司[1]。联系人[0]。联系人姓名:Jane Doe

再见世界

实体:

public class Company
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Promo { get; set; } 
    public virtual List<Contact> Contacts { get; set; }       
}

public class Contact
{
    [Key]
    public int ContactID { get; set; }
    [ForeignKey("Company")]
    public int CompanyID { get; set; }
    public virtual Company Company { get; set; }
    public string ContactName { get; set; }
    public string ContactNumber { get; set; }
}
上市公司
{
[关键]
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串Promo{get;set;}
公共虚拟列表联系人{get;set;}
}
公共类联系人
{
[关键]
public int ContactID{get;set;}
[外键(“公司”)]
public int CompanyID{get;set;}
公共虚拟公司公司{get;set;}
公共字符串ContactName{get;set;}
公共字符串ContactNumber{get;set;}
}
测试线束:

public static void Main()
{
    Console.WriteLine("Hello World");
    FakeDbContext context = new FakeDbContext();
    var companiesData = new Company[]
    {
        new Company
        {
            Name = "Some name", Promo="Y", Contacts = new List<Contact> { new Contact {ContactName="John Doe", ContactNumber="(828)292-2912", CompanyID=1}},
        },
        new Company
        {
            Name = "Another name", Promo="N",  Contacts = new List<Contact> { new Contact {ContactName="Jane Doe", ContactNumber="(828)292-2912", CompanyID=2}},
        },
    };
    foreach (Company c in companiesData)
    {
        context.Companies.Add(c);
        foreach (Contact contact in c.Contacts)
        {
            context.Contacts.Add(contact);  
        }
    }
    context.SaveChanges();
    Console.WriteLine("Save done.");
    var companies = context.Companies.ToList();
    Console.WriteLine("companies.Count: " + companies.Count);
    for (int i = 0; i < companies.Count; i++)
    {
        Console.WriteLine(string.Format("company[{0}].Name: {1}", i,     companies[i].Name));
        for (int j = 0; j < companies[i].Contacts.Count; j++)
        {               
Console.WriteLine(string.Format("company[{0}].Contacts[{1}].ContactName: {2}", i, j, companies[i].Contacts[j].ContactName));
        }
    }
    Console.WriteLine("Bye World");
}
publicstaticvoidmain()
{
Console.WriteLine(“你好世界”);
FakeDbContext=new FakeDbContext();
var CompanyData=新公司[]
{
新公司
{
Name=“Some Name”,Promo=“Y”,Contacts=new List{new Contact{ContactName=“John Doe”,ContactNumber=“(828)292-2912”,CompanyID=1},
},
新公司
{
Name=“另一个名字”,Promo=“N”,Contacts=new List{new Contact{ContactName=“Jane Doe”,ContactNumber=“(828)292-2912”,CompanyID=2}},
},
};
foreach(公司数据中的公司c)
{
上下文。公司。添加(c);
foreach(c.触点中的触点触点)
{
context.Contacts.Add(contact);
}
}
SaveChanges();
Console.WriteLine(“保存完成”);
var companys=context.companys.ToList();
Console.WriteLine(“companys.Count:+companys.Count”);
对于(int i=0;i
输出:

你好,世界

保存完成

公司。计数:2

公司[0]。名称:某个名称

公司[0]。联系人[0]。联系人姓名:John Doe

公司[1]。名称:其他名称

公司[1]。联系人[0]。联系人姓名:Jane Doe


拜拜世界

大家好,在我的最后一个项目中,我对viewmodel也做了同样的操作,因为它是来自两个不同表的数据,在将来可以更自由地改变想法。所以我创建了一个视图模型

public class ViewModelContactCompany
{
     public String Name { get; set; }
     List<Contact> contacts { get; set; }
    //etc just a sample
}
}

现在来看

@model /patch.ViewModelContactCompany

and here you get the data on the object return on the controler

嗨,在我的最后一个项目中,我对v也做了同样的事情
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }

    public DbSet<Company> Companies { get; set; }
    public DbSet<Contact> Contacts { get; set; }
}
@model List<Company>

<h3>Do something with 'Model' here</h3>