问题
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
回答1:
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:
来源:https://stackoverflow.com/questions/9692398/dynatree-asp-net-mvc-how-do-you-post-data-back-to-server