How to create a recursive structure in ASP.NET MVC

I have a category table that has three fields: Id , Title and ParentId . I would like to create a recursive hierarchical structure of my table (tree) in a cshtml file. I am new to ASP.NET MVC and I don’t know how to do this because there is no code file and I don’t know where to start. Note that I am storing a jungle , not a tree, in my database. In other words, the result tree can have many roots.

+6
source share
1 answer

The easiest is to use an assistant:

 @helper RecurseSomething(MyClass data) { <li> @data.Title @if (data.SubItems.Count() > 0) { <ul> @foreach(var subData in data.SubItems) { @RecurseSomething(subData); } </ul> } </li> } 
+11
source

Source: https://habr.com/ru/post/891697/


All Articles