treeview

Nov 22, 2012 at 3:20 PM

Hi,

I'm very new to mvc controls toolkit and Lamba expressions but I woul like to use the Treeview. I was looking into the documentation of the treeview but it couldn't help me very much.

therefore I need your help!

The treeview should be a part of a page with some other information. Therefore i used this _Personnel model. Everything works fine except for the Locations treeview so you don't need to consider personnel/personnelproperty. (I used entity framework to build the models)

namespace Entity.Models
{
    public class _Personnel
    {
        public List<Entity.Personnel> personnel { get; set; }
        public List<Entity.PersonnelProperty> personnelproperty { get; set; }
        public List<Entity.Location> Locations { get; set; }
    }
}

The table Location has the following columns:

 

LocationID int
Description string
ParentLocationID int

When i execute the following code in Personnel.cshtml it gives me a list of all the locations so the list is properly filled.

    @foreach(var item in Model.Locations) {
  • @item.Description }

 

Now i want to use the the Treeview in my Personnel.cshtml page. I tried to copy it from the documentation and this is what i got:

 

@Html.TreeViewFor(m => Model.Locations, l => "ParentLocationID".Equals("1")? "children":null , ExternalContainerType.span,
                                        "null",new object[]{ _S.L<Entity.Location>(h => "" + h.DisplayFor(m => m.Description).ToString()+""),
                                               _S.L<Entity.Models.Location>(h => "" +h.DisplayFor(m => m.Description).ToString()+"") },
                                               (x, y) => x is Entity.Location ? 0 : 1, "filetree treeview-red",
                                                    null,
                                                    null,
                                                    TreeViewMode.InitializeDisplay,
                                                    (x) => "allenodes",
                                                    (x,y) => TreeViewItemStatus.initializeShow
                                            );

At least no syntax error but when i execute this it gives me a treelist with only 1 child/parent level:

 

<ul>
    <li>location 1</li>
    <li>location 1.1</li>
    <li>location 2</li>
</ul>

instead of 

<ul>
    <li>location 1
        <ul>
            <li>location 1.1</li>
        </ul>
    </li>
    <li>location 2</li>
</ul>

Can you please help me with this?

Thanks in advance!

Coordinator
Nov 24, 2012 at 4:19 PM

 

"ParentLocationID".Equals("1") will always fail! maybe you want to mean l.ParentLocationId == 1. 
The l of the lambda is the current object and in this expression you have to return the name of the collection (as a string) containing
all children as a function of the current object l.
Moreover all objects used in the three MUST implement the interface ISafeCreation rof security reasons. There is no member to implement
so your class must just inherit from ISafeCreation as explained in the documentation.