title: Can I write a function in Blazor that dynamically renders elements in a View?
body: <p>I have an object called BusinessUnit which contains a list of child business units and I need a function that renders &lt; li> elements for every child under its parent. The code I have so far is below:</p>
  
  <pre><code>&lt;ul id="Level0"&gt;
      @foreach (var bizUnit in businessUnitViewModel.BusinessUnitInfos)
      {
          &lt;li&gt;
              &lt;span&gt;@bizUnit.BusinessUnitName&lt;/span&gt;
                  &lt;ul class="nested"&gt;
                      @foreach (var childOfbizUnit in bizUnit.Children)
                      {
                          &lt;li&gt;@childOfbizUnit.BusinessUnitName&lt;/li&gt;
                      }
                  &lt;/ul&gt;
          &lt;/li&gt;
      }
  &lt;/ul&gt;
  </code></pre>
  
  <p>The nested foreach is basically the same as the first one, but hardcoding them limits how many hierarchical levels I can have. I need a function like this:</p>
  
  <pre><code> public void HasKids(BusinessUnitInfo bizUnit)
      {
          foreach (var bizUnitChild in bizUnit.Children)
          {
              //Render an &lt;li&gt; tag element with bizUnitChild's 
              name&lt;li&gt;bizUnitChild.Name&lt;/li&gt;
  
              HasKids(bizUnitChild);
          }
      }
  </code></pre>
  
  <p>Does anyone know what I can do for the comment in the last code block; can I use C# code to dynamically render a list tag? Thans :)</p>