ASP.Net MVC: Recursive in the MVC View.

We have used recursive method i the c# code, but have you ever had a requirement where you have to render a view from a recursive data. I got on requirement of that like and i have to render the view data from a tree based data. so for this you need to do like this in your view.

@model List<TreeViewModel>
@{

    View.Title = "TreeData";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Tree Data</h2>

@ParseTree(Model, null);

@helper ParseTree(IEnumerable<TreeViewModel> nodes, int? parentId) {
    if (nodes.Any(n => n.ParentId == parentId)) {
    <ul>
        @foreach (var node in nodes.Where(n => n.ParentId == parentId)) {
            <li>
                @node.DisplayName
                @ParseTree(nodes, node.Id)
            </li>
        }
    </ul>
    }

Please go through this and leave feedback.

Comments

Popular posts from this blog

LINQ: Using int.TryParse() within a LINQ where clause

ASP.Net MVC: Configuring asp.net session state in SQL Server.

IIS: Publish Project to remote IIS with Web Deploy.