开发者

How to create a recursive structure in ASP.NET MVC

开发者 https://www.devze.com 2023-03-16 20:58 出处:网络
I have a categories table which has three fields: Id, Title, and ParentId. I\'d like to create a recursive hierarchical structure of my table (a tree) in a cshtml file. I\'m new to ASP.NET MVC and I d

I have a categories table which has three fields: Id, Title, and ParentId. I'd like to create a recursive hierarchical structure of my table (a tree) in a cshtml file. I'm new to ASP.NET MVC and I don't know how to do that, because there is no code-behind file and I don't know from where sh开发者_高级运维ould I start. Please note that I'm storing a jungle, not a tree in my database. In other words, the result tree can have many roots.


The easiest is using a helper:

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

精彩评论

暂无评论...
验证码 换一张
取 消