If you want a server side solution, you should be able to get something useful from this article I wrote on using recursion in ASP.NET Web Pages: http://www.mikesdotnetting.com/Article/208/Practical-Recursion-in-ASP.NET-Web-Pages-with-WebMatrix.
But in essence, you need a helper something like this:
@helper BuildTreeView(IEnumerable<dynamic> data, int? parentid = null, int level = 0) {
var nodes = data.Where(n => n.ParentId == parentid).OrderBy(n => n.DisplayOrder);
if (nodes.Any()) {
if(nodes.First().ParentId != null){
level++;
}
foreach (var node in nodes) {
<div style="padding-left:@(level * 20)px;">
@node.Text
</div>
@BuildTreeView(data, node.Id, level);
}
}
}
I've added a DisplayOrder column so that you can add and remove items from your database and still control the order in which they are displayed in their individual nodes.