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
@model IList
@foreach (var Item in Model)
{
-
@Item.CategoriaNome
@if (!(Item.SubCategorias == null))
{
@foreach (var subcategoria in Item.SubCategorias)
{
- @subcategoria.Nome
}
}
}
The classes
Category
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
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 AlshabaniPosted Aug 9, 2021, 7:46 PM
Read about different types of loading down here https://docs.microsoft.com/en-us/ef/ef6/querying/related-data
Andre SilveiraPosted Aug 9, 2021, 8:22 PM