I have an issue that usual System.Web.UI.WebControls with @(Html.TreeView(Model) is not implemented in ASP.NET Core. Is it possible to a implement hierarchy tree view with recursion using only Razor, html?

There is my tree node model:

public class EmployeeTree
{
    public EmployeeTree()
    {
        Reporters = new List<EmployeeTree>();
    }
    public int Id { get; set; }
    public int? ManagerId { get; set; } 
    [Required]
    [MaxLength(30)]
    public string FirstName { get; set; }
    [Required]
    [MaxLength(30)]
    public string SecondName { get; set; }
    [Required]
    [MaxLength(30)]
    public string Position { get; set; }
    public string Phone { get; set; }

    public virtual EmployeeTree Manager { get; set; }
    public virtual List<EmployeeTree> Reporters { get; set; }
}

I tried such recursion Razor with @functions helper, but it did not load child elements. Any suggestions, whats wrong?

@using System.Text

@functions{
    void GetEmployeeTree(EmployeeTree data)
    {
        StringBuilder output = new StringBuilder();
        output.Append("Employee");
        output.Append("<li>");
        output.Append(data.FirstName);
        if (data.Reporters.Count > 0)
        {
            foreach (var dataNode in data.Reporters)
            {
                output.Append("<ul>");
                output.Append(dataNode.FirstName);
                GetEmployeeTree(dataNode);
                output.Append("</ul>");
            }
        }
        output.Append("</li>");
    }
}

0 Answers