Hi all,
I have a class "Category" with a List
How do I list the category and subcategory?
The code bellow is not listing the subcategorys
Python
@model IList
@foreach (var Item in Model)
{
@Item.CategoriaNome
@if (!(Item.SubCategorias == null))
{
@foreach (var subcategoria in Item.SubCategorias)
{
@subcategoria.Nome
}
}
}The classes
Category
C#
public class Category
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "Nome obrigatório")]
[Display(Name = "Nome da categoria")]
public string CategoriaNome { get; set; }
[Display(Name = "Idioma:")]
public string Idioma { get; set; }
public List SubCategorias { get; set; }
public List Things{ get; set; }
}SubCategory
C#
public class SubCategory
{
[Key]
public int Id { get; set; }
public string Nome { get; set; }
public string Idioma { get; set; }
public int CategoriaId { get; set; }
[ForeignKey("CategoriaId")]
public Category Categoria { get; set; }
public List Things{ get; set; }
}Thanks in advance

Basem AlshabaniRank #3,323 · 432 reputationPosted Aug 9, 2021, 3:46 PM EDT
Read about different types of loading down here https://docs.microsoft.com/en-us/ef/ef6/querying/related-data
Andre Silveira154 reputationPosted Aug 9, 2021, 4:22 PM EDT