Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// This is the model class
- public class MyTreeNode<T>
- {
- public ICollection<MyTreeNode> ChildNodes {get;}
- public T MyValue { get; set; }
- bool HasChildren { get { return ChildNodes.Any(); } }
- }
- ///This is the html helper:
- public static string RenderTree<T>(this HtmlHelper html, MyTreeNode<T> root, Func<T, string> renderNode)
- {
- var sb = new StringBuilder();
- sb.Append(renderNode(root.MyValue));
- if (root.HasChildren)
- {
- foreach(var child in root.ChildNodes)
- {
- sb.Append(RenderTree<T>(html, child, renderNode));
- }
- }
- return sb.ToString();
- }
Add Comment
Please, Sign In to add comment