Is there a way to create a recursive helper in the Razor generator template

I created a template using the Razor Generator . I now need a recursive function to create a nested list of items. I tried this solution , but then all my codes became marked as errors.

@* Generator: Template *@ @functions { public IList<Models.Category> Topics { get; set; } } @helper ShowTree(IList<Models.Category> topics) { <ul> @foreach (var topic in topics) { <li> @topic.Title @if (topic.Childs.Count > 0) { @{ ShowTree(topic.Childs); } } </li> } </ul> } 

Some of the irrelevant errors I received after adding the helper:

 -Error 3 Invalid expression term ';' Error 4 Feature 'lambda expression' cannot be used because it is not part of the ISO-2 C# language specification Error 13 Feature 'implicitly typed local variable' cannot be used because it is not part of the System C# language specification Error 6 The name 'WriteLiteralTo' does not exist in the current context 

but when I remove the helper method, it all just disappears!

Am I doing something wrong or is creating helpers in Razor templates impossible?

+4
source share
2 answers

The code below will work.

 @helper ShowTree(IList<Models.Category> topics) { if (topics != null && topics.Any()) { <ul> @foreach (var topic in topics) { <li> @topic.Title @ShowTree(topic.Childs) </li> } </ul> } } 
0
source

Why is it worth it, since no one gave a better answer, we refused to use RazorGenerator templates for this reason.

My question is here , and our answer was to set up a local MVC website on an internal server and use it to render our templates. This is the only offer I have seen yet that worked the way I wanted.

0
source

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


All Articles