0

I'm working on an angularjs project with asp.net web api. I have sample data which I need to replicate as my database model.

var countries = [
    {
        name: "UK",
        cities: [
            {name: "London"},
            {name: "Manchester"},
            {name: "Birmingham"},
        ]
    }, 

    {
        name: "USA",
        cities: [
            {name: "Los Angeles"},
            {name: "Chicago"},
            {name: "Houston"},
        ]   
    }, 

    {
        name: "India",
        cities: [
            {name: "Hyderabad"},
            {name: "Chennai"},
            {name: "Mumbai"},
        ]   
    }
];

I need this model to created nested content when data is pulled by my angular js service.

Guzzyman
  • 561
  • 6
  • 16
  • 37

1 Answers1

0

You must create your Models as C# classes

Country.cs

namespace WebApplication1.Clases
{
    public class Country
    {
        public String name { get; set; }
        public City[] cities { get; set; }
    }
}

City.cs

namespace WebApplication1.Clases
{
    public class City
    {
        public String name { get; set; }
    }
}
Brank Victoria
  • 1,447
  • 10
  • 17
  • Yes I know I'll have to create my models as c# classes but for the city in the country class, would it be written as public IEnumerable Cities {get; set;} – Guzzyman Jul 17 '18 at 10:41
  • I guess this all depends of what you need and how you will use the cities attribute, see this question to know which one fits better for you. https://stackoverflow.com/questions/764748/whats-the-difference-between-ienumerable-and-array-ilist-and-list – Brank Victoria Jul 17 '18 at 10:44
  • Nice article but it didn't address my need. Let em explain further: I have two list with one been the parent just like country is the parent and the other been the child. The parent has 225 records while the child has about 4000 records. The is categorized into the ids of the parent hence we have 225 parent records with their respective child records. The representation I gave about is exactly the nesting I want with parent child relationship. How do I represent this in poco classes since I use code-first migration and would want to update my tables accordingly. – Guzzyman Jul 17 '18 at 11:26