On Sep 10, 2:26*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
shapper <mdmo...@gmail.comwrote:
What is the best way to create a structure (List) with parent and
child? For example:
Home
* Contact
Products
* *Books
* *Magazines
* * * *Sport
* * * *Travel
Basically I want to store a menu structure into an object and then
loop through it and build the HTML.
How can I do this?
Firstly, if you're researching this you'll want to get the right name -
this isn't a list, it's a tree.
Now, as for implementing it, assuming you want the same content type at
all locations, you could do something along the lines of:
public class TreeNode<T>
{
* * T content;
* * TreeNode<Tparent; // Will be null for the root
* * IList<TreeNode<T>children;
}
What API you put on top of that is up to you, of course.
--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Basically, I need to render in my MVC project the following:
<ul id="Menu" class="Menu-Smog">
<li><%= Html.ActionLink("home", "Index", "Home") %></li>
<li>
<a href="#CMS">Cms</a>
<ul>
<li><%= Html.ActionLink("Create Post", "Create", "Post") %></li>
<li><%= Html.ActionLink("Delete File", "Delete", "File") %></li>
</ul>
</li>
<li><%= Html.ActionLink("Contact", "Contact", "Home") %></li>
</ul>
So basically I each Tree Node should render as follows:
1. Is Not Active
<li><a href="#CMS">Cms</a></li>
2. Is Not Active but has child items
<li>
<a href="#CMS">Cms</a>
<ul>
### Child Items Go Here ###
</ul>
</li>
3. Is Active and does not have child elements
<li><%= Html.ActionLink( { Link Name }, { Action },
{ Controller } ) %></li>
4. Is Active and has child elements:
<li>
<li><%= Html.ActionLink( { Link Name }, { Action },
{ Controller } ) %></li>
<ul>
### Child Items Go Here ###
</ul>
</li>
So each Tree Node should have the following properties: ID, IsActive,
Name, Action and Controller
And the ability to check if it contains child items and if there is
loop through their child items applying the same rules ...
My problem is more with how the object should be and how to loop
through the Tree and follow this rules ...
Thanks,
Miguel