I managed to display a folder tree with Parent/Child relationships between them. But the way I did it is really not optimized and very ugly.
I have an @model which contains items with the following attributes:
- Id (item id)
- Libelle (item name)
- Pere (Id of the parent of the item)
- Niveau (Place of the item in the hierarchy of the tree)
Here is the code that works:
@foreach (var item in Model)
{
<div class="bloc-mise-en-page">
<div id="arbo-dossier-2">
@if (item.Niveau == 1)
{
<ul>
<li>
<input type="checkbox" id=@item.Libelle><label for=@item.Libelle>@item.Libelle</label>
@foreach (var item1 in Model)
{
@if ((item1.Niveau == 2) && (item1.Pere == item.Id))
{
<ul>
<li>
<input type="checkbox" id=@item1.Libelle><label for=@item1.Libelle>@item1.Libelle</label>
@foreach (var item2 in Model)
{
@if ((item2.Niveau == 3) && (item2.Pere == item1.Id))
{
<ul>
<li>
<input type="checkbox" id=@item2.Libelle><label for=@item2.Libelle>@item2.Libelle</label>
</li>
</ul>
}
}
</li>
</ul>
}
}
</li>
</ul>
}
</div>
</div>
}
What is the most optimized way to do this? Can we do recursive in html? Thank you for your reply