1

I am using asp.net mvc 3 and looking at possibly using dynatree. I am in the research page still and trying to figure out some outstanding questions before I start actually implementing it.

Dynatree with ASP.NET MVC

I been looking at the above post and how @Matt Penner renders his tree view and in my opion looks cleaner than the accepted answer.

One question I have though is how do you post the checked options to the server? When it gets rendered it seems to all be bullet tags that get converted to look like checkboxes.

So I am wondering how do I serialize that data? Right now I have a form and when the user hits submit I do a jquery serializeArrary and send it via ajax to the server where it gets binded to my View Model.

How would I do it for the choices selected in the tree view?

I am also not clear on how I could render the tree view with default checked nodes based on the example Matt Penner has.

Thanks

Community
  • 1
  • 1
chobo2
  • 83,322
  • 195
  • 530
  • 832

1 Answers1

2

The way Matt Penner renders the tree is simpler and cleaner...but since ha has not built "models" that represents the tree data, it is not easy to use his approach to read back the tree. The tree must be read back into a "model", and since it is a tree such model must be recursive as the one shown in the first example.

However, if you give adequate names to the checkboxes you can read them back. In fact model binding on post is done by using a name convention, so for instance a checkbox named:

Children[2].Children[0].MyCheckBox

Will be read back in the: third children first level => first chidren second level model of the herarchical model you have to build to receive the posted data.

However this approach works well, if the user doesn't modify the structure of the tree, because in such a case the checkbox named Children[2].Children[0].MyCheckBox may come to a different place that its name suggests, that is it would be no more the 3 child firts level, and might become, for instance the 5th child first level...but its name doesn't change.

On the other side if you serialize the data on the client and then send them in json to the sever, you will be able to take into account also tree modifications.

That said give a look to the TreeView of the Mvc Controls Toolkit (I am the coordinator of the project)...it do already all job for you,and you can use a generic template for each node, that is you can put everuthing you like in each node:

Francesco Abbruzzese
  • 4,139
  • 1
  • 17
  • 18
  • This is intresting but I am not sure if this is something I like to use. I am not too crazy about this huge html helpers as it makes it harder for the designers come through. I rather stick to like using the checkboxFor helper and just a regular unordered list tag. – chobo2 Mar 27 '12 at 16:40
  • The treeView just displays unordered list containing your nodes templates+some hidden fields that it needs to work infos, so you have full control on the html that is created. Anyway, if you want write yourself your full Html so you can interact better with your designer, the alternative is to write your nested
      forgetting about model binding. On post you invoke a js function that do a recursive visit of the tree and store the infos about the cheched checkboxes in a hierarchical model. Then you serialize it in json, put into an hidden fleld, and once on the server deserialize it.
    – Francesco Abbruzzese Mar 27 '12 at 16:56
  • Can you give a more detailed example. I don't think I need to drop model binding for what I need to do. – chobo2 Mar 27 '12 at 19:23
  • As I said There two enough simple solutions: 1) giving adequate names to checkboxes and relying on default model binder. However this can be done only if the structure of the tree doesn't change 2) serializing the information contained in the tree in Json and sending it to the server. Now solution 1 is simpler. Does your tree changes is structure, that is do you need to move branches from a place to another and/or add/delete nodes? Pls answer this question and I will provide an example of either 1) or 2) according to your needs. – Francesco Abbruzzese Mar 28 '12 at 10:47
  • Bummer. It depends on jQuery.Treeview which is no longer maintained. – Phil Jul 09 '13 at 11:35