For my small web app project I've created a model called Company that includes basic company info & also a list of sales reps from different business partners. This is my Contact Model:
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; }
}
}
This is how I pass the data into my local database:
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();
How do I load the contact list items onto the razor view? I'm looking at my database and it's showing a blank field for "Contacts". The rest of the data shows up just fine.