ASP.NET MVC 3 Razor recursive function

后端 未结 2 1481
不知归路
不知归路 2020-12-04 07:02

Okay, so I want to display a list containing lists of lists of lists...

I have no way of knowing how many levels there are to display, so I figured this is where I b

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 07:47

    The Razor view engine allows to write inline recursive helpers with the @helper keyword.

    @helper ShowTree(IEnumerable foos)
    {
        
      @foreach (var foo in foos) {
    • @foo.Title @if (foo.Children.Any()) { @ShowTree(foo.Children) }
    • }
    }

提交回复
热议问题