The first step crafting an View Model that fits represents the UI you are trying to achieve.
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
public List<State> States { get; set; }
}
public class State
{
public int Id { get; set; }
public string Name { get; set; }
}
Fill the view model and display the view model.
foreach(Country country in countries)
{
//Write Country
Console.WriteLine(country.Name);
foreach(State state in country.States)
{
//Write state
Console.WriteLine($" {state.Name}");
}
}
These concepts are covered in EF Core fundamentals.
Member
75 Points
161 Posts
Nested Forloop in asp.net core
Oct 04, 2019 03:27 PM|Prathamesh Shende|LINK
Hi,
i have two table
Country and State which is also connected to each other using FK PK.
I want to represent the data in this form
United States
California
NewYork
India
Delhi
Panjab
I think structure will be like this
Foreach (Country)
{
Foreach(State)
}
How can i code this ? I tried but its not work by me. please tell me how can i implement this.
All-Star
52961 Points
23564 Posts
Re: Nested Forloop in asp.net core
Oct 04, 2019 04:30 PM|mgebhard|LINK
The first step crafting an View Model that fits represents the UI you are trying to achieve.
Fill the view model and display the view model.
These concepts are covered in EF Core fundamentals.
Member
75 Points
161 Posts
Re: Nested Forloop in asp.net core
Oct 04, 2019 06:07 PM|Prathamesh Shende|LINK
its not working sir. I think i made mistake while putting code.
can you please tell me where do i place this ? What is 'countries' ? I got this Country is coming from ViewModel
Please help me, I am new on .net core
Thank You
All-Star
18815 Points
3831 Posts
Re: Nested Forloop in asp.net core
Oct 07, 2019 05:07 AM|Nan Yu|LINK
Hi Prathamesh shende ,
I would suggest you first read how to enable relationship in EF Core :
Configuring One To Many Relationships in Entity Framework Core
So that you can read with navigation property easily :
https://www.entityframeworktutorial.net/efcore/one-to-many-conventions-entity-framework-core.aspx
https://www.learnentityframeworkcore.com/conventions/one-to-many-relationship
Best Regards,
Nan Yu