This project is read-only.

Treeview with SQL Database Source?

Nov 10, 2012 at 1:00 PM
Very cool control. I'm using ASP.NET MVC 4 and MSSQL database. I am new to MVC 4 and have been coding ASP.NET for many years. Q. How do I connect to the SQL database (need code). Q. How do I Add, Edit, Delete parent or child Treeview nodes (need code)?
Coordinator
Nov 10, 2012 at 2:42 PM

In Mvc there is no concept of data source you have to read an bject tree from the database and putting it in your ViewModel, so in the view you can pass it to your tree. In order to save an object tree in a database, if all noded belong to the same table you have to define a database relation of the table with itself that is one-to-many and represents father-children.

To read the whole tree ftom the DB with linq it is enough: 

var allNodes= (from x in context.MyTable select x).ToList();

Then to select all top level nodes (the one without a father: allNodes.Where(x => x.Parent == null). What you get this way can be passed to the treeview: it is the list of all root nodes. All nodes will have automatically the list of children array filled by linQ.