New Child in TreeView

Jun 6, 2011 at 1:20 PM

Hi, I'm trying to use the TreeViewFor to edit the details in a DetailFormFor, but when I add new items I get exceptions (in "jquery-1.6.1.min.js" or in "MVCControlToolkit.Controls-1.1.0.min.js") and sometimes it postback null to the tree items (m => m.Usuarios). If I edit or delete items it works. Can you help me?


            @Html.TreeViewFor(
                m => m.Usuarios,
                i => "items",
                ExternalContainerType.span,
                "filetree treeview-black",
                new object[]
                {
                    _S.L<ProjAprovTreeView>(h => h.DisplayFor(m => m.usuario_id).ToString())
                },
                (x, y) => 0,
                "filetree treeview-black",
                new object[]
                {             
                    _S.L<ProjAprovTreeView>(h => string.Format(
                        "<div class='usuario' >{0} {1} {2}</div>", 
                        h.EditorFor(m => m.usuario_id).ToString(),
                        h.TreeViewDeleteButton(Url.Content("~/Content/themes/test/images/delete.png"), 
                            ManipulationButtonStyle.Image).ToString(), 
                        h.TreeViewAddButton(0, Url.Content("~/Content/themes/test/images/add.png"), 
                            ManipulationButtonStyle.Image).ToString()))
                },
                (x, y) => 0,
                TreeViewMode.InitializeDisplay,
                (x) => "allnodes",
                (x, y) => TreeViewItemStatus.initializeShow,
                new TreeViewOptions() { 
                    Animated = 1,
                    Unique= false,
                    Opacity = 1,
                    CanMove = true,
                    CanAdd = true,
                    Persist = TreeViewPersistencyMode.EditOnly,
                    CookieId = "treeview"
                })
                @Html.TreeViewToggleEditButtonFor(
                 m => m.Usuarios,
                 "Editar Usuários""TreeViewEdit",
                 "Desfazer""TreeViewUndo",
                 "Refazer""TreeViewRedo")
My Class:
    public class ProjAprovTreeView
    {
        public int usuario_id { getset; }
 
        public List<ProjAprovTreeView> items { getset; }
    }
Coordinator
Jun 7, 2011 at 8:23 AM

I will try your code. Actually, we have modified template representation to work also with ajax just in the last releases(before you were not allowed to performs add in controls returned by ajax calls), however there might be some issues that still create problems. This means your code might be correct but there might be still a bug when using new item templates(that is addition of new items) in html returned by ajax calls. I'll let you know, and if there is a bug a will post a fix asap.

In the meantime I suggest you use razor helpers instead of lambda expressions to define the item templates if you use razor as view engine! they are easier to use, and you take more advantage of intellisense. LAmbda expressions templates are for the ones not using Razor.

Jun 7, 2011 at 10:50 AM

Thank you! I will try use Razor.

Coordinator
Jun 7, 2011 at 6:11 PM

hI Brunio,

As I already announced, actually the wrong behaviour you observed is due to a bug. We have already fixed it. 

I will be glad to you if you might try our fix on your code, so we will have a further test before preparing a bug fix release. You need just to substitute the main js file of the toolkit. 

If you would like to cooperate and test the fix on your cod, please send me a message on the contact form of my Blog, I will sent the js file in the reply to your message. Below the contact form of my blog:

http://www.dotnet-programming.com/contact.aspx

 

Thanks in advance

Jun 10, 2011 at 5:30 PM

Hi,

Now I'm with a new problem: how can I add items to a empty treeview? I have a treeview where Mode != null and Model.Count == 0.

See ya.

Coordinator
Jun 10, 2011 at 7:30 PM
Edited Jun 10, 2011 at 7:31 PM

YOU CANT! A grid with no rows is just an empty grid, but an empry tree is ....NOTHING.....a NULL pointer! :)

The grid is the container of its rows...but in the case of a tree each node is the container of its children, so: no nodes = no container at all = just a null pointer :)

You must have at least one root node. Just to make an example, consider the the tree of folders you have in an Email client....You can delete all nodes, but th initial root node that contains all folder CANNOT BE DELETED!

 

You can always add a ROOT node whose only purpose is that of OVERALL container. Consider that all nodes of a tree may be different datatypes, so you can define a root node that the user cannot delete and you can display it with an ad hoc template. You can also change the icon used to represent it b editing the ree css file and assing one more css rule just for the root node.

If you take the tree from a database, you don't need t store this root node in the databas, but you can add it after having retrieved all nodes from the database.

 

P.S. Nex time you would like to start a new discussion please ...just start a new discussion do not use an old discussion. Discussions we do here are useful also to other people that have similar problems, and the codeplex site has a search engine to find "discussions" related to a topic, so if you add a new topic to an old discussion ...it will be difficult for someone else to find that topic. :)