{"id":11353,"date":"2016-09-14T09:19:00","date_gmt":"2016-09-14T09:19:00","guid":{"rendered":"https:\/\/viewmyprojects.com\/winwirewp\/?p=11353"},"modified":"2025-01-29T10:40:59","modified_gmt":"2025-01-29T10:40:59","slug":"how-to-create-tree-view-using-asp-net-mvc","status":"publish","type":"post","link":"https:\/\/viewmyprojects.com\/winwirewp\/blog\/how-to-create-tree-view-using-asp-net-mvc\/","title":{"rendered":"How to Create Tree view using ASP.NET MVC"},"content":{"rendered":"\n<p>One of the frequently used control is Tree view and Microsoft doesn\u2019t have this in the default set of controls. We have to depend on the third party\/JQuery controls. One of the free control is aciTree and can be download from Nuget. aciTree is a customizable JavaScript tree view control written as a jQuery plugin. It offers an easy to use API and it has built-in functionality to load the entire tree with AJAX.<\/p>\n\n\n\n<p><em><strong>How to use aciTree in MVC<br><\/strong><\/em><br><strong>1. Add the reference to set of aciTree JavaScript and CSS file<br><\/strong><br>a. aciTree.css<br>b. jquery.min.js version should be &gt;= 1.11.1<br>c. jquery.aciPlugin.min.js<br>d. jquery.aciTree.dom.js<br>e. jquery.aciTree.core.js (Optional)<br>f. jquery.aciTree.selectable.js (Optional)<br>g. jquery.aciTree.checkbox.js<br>h. jquery.aciTree.radio.js<br>i. jquery.aciTree.min.js<\/p>\n\n\n\n<p><strong>2. Declare the search box if search functionality is required<br><\/strong><br>Search: &lt;input id=\u201dsearch\u201d type=\u201dtext\u201d value=\u201d\u201d name=\u201dsearch\u201d&gt;<\/p>\n\n\n\n<p><strong>3. Declare div with id and CSS classes<br><\/strong><br>&lt;div id=\u201dtree\u201d class=\u201daciTree\u201d&gt;&lt;\/div&gt;<\/p>\n\n\n\n<p><strong>4. Call the WEB-API method for data and it should be return in JSON format<br><\/strong><br>$(\u2018#tree\u2019).aciTree({<br>ajax: {<br>\/\/Calling controller method<br>url: \u2018\/Home\/GetTreeView\u2019<br>}<\/p>\n\n\n\n<p><strong>5. If we need checkbox, radio button at node level then set checkbox: true , radio: true<br><\/strong><br><strong>Complete code for search, checkbox, radio button and link at leaf node<br><\/strong><br>&lt;script class=\u201dcode\u201d type=\u201dtext\/javascript\u201d&gt;<\/p>\n\n\n\n<p>$(function () {<\/p>\n\n\n\n<p>\/\/ init the tree<br>$(\u2018#tree\u2019).aciTree({<br>ajax: {<br>\/\/Calling controller method<br>url: \u2018\/Home\/GetTreeView\u2019<br>},<br>\/\/this code is for making leafe lavel node as link<br>itemHook: function (parent, item, itemData, level) {<br>if (!itemData.inode) {<br>\/\/ a custom item implementation to show a link<br>this.setLabel(item, {<br>label: \u2018&lt;a href=\u201d\u2018 + itemData[\u2018myurl\u2019] + \u2018\u201d target=\u201d_blank\u201d title=\u201dClick to open \u2018 + itemData[\u2018myurl\u2019] + \u2018\u201d&gt;\u2019 + itemData.label + \u2018&lt;\/a&gt;\u2019<br>});<br>}<br>},<br>\/\/if we need check box then just set it as true<br>checkbox: true,<br>radio: true,<\/p>\n\n\n\n<p>\/\/ our custom filter\/search<br>filterHook: function (item, search, regexp) {<br>if (search.length) {<br>\/\/ try to get the parent<br>var parent = this.parent(item);<br>if (parent.length) {<br>\/\/ get parent label<br>var label = this.getLabel(parent);<br>if (regexp.test(String(label))) {<br>\/\/ all direct childrens match<br>return true;<br>}<br>}<br>\/\/ match the item<br>return regexp.test(String(this.getLabel(item)));<br>} else {<br>\/\/ empty search, all matches<br>return true;<br>}<br>},<br>});<\/p>\n\n\n\n<p>var api = $(\u2018#tree\u2019).aciTree(\u2018api\u2019);<\/p>\n\n\n\n<p>$(\u2018#search\u2019).val(\u201d)<br>var last = \u201d;<br>\/\/its for searching the information<br>$(\u2018#search\u2019).keyup(function () {<br>if ($(this).val() == last) {<br>return;<br>}<br>last = $(this).val();<br>api.filter(null, {<br>search: $(this).val(),<br>success: function (item, options) {<br>if (!options.first) {<br>alert(\u2018No results found!\u2019);<br>}<br>}<br>});<br>});<br>});<br>&lt;\/script&gt;<\/p>\n\n\n\n<p><strong>JSON data format<br><\/strong><br><strong>1. Need to create a model class for n level tree with following properties<\/strong><\/p>\n\n\n\n<p>public class TreeNode<br>{<br>public int id { get; set; }<\/p>\n\n\n\n<p>public int? parent { get; set; }<\/p>\n\n\n\n<p>public string label { get; set; }<br>public bool inode { get; set; } \/\/this if for if node have any child node<br>public bool open { get; set; } \/\/this is for opening the node by default<br>public bool checkbox { get; set; } \/\/ this is for displaying checkbox at node<br>public bool radio { get; set; } \/\/this is for displaying radio button at node<\/p>\n\n\n\n<p>public string myurl { get; set; } \/\/ this is for setting URL for navigation<br>public List&lt;TreeNode&gt; branch { get; set; } \/\/ this is for child nodes<\/p>\n\n\n\n<p>public TreeNode()<br>{<br>branch = new List&lt;TreeNode&gt;();<br>}<br>}<\/p>\n\n\n\n<p><strong>2. Need to return data in JSON format to view (Controller method)<br><\/strong><br>public JsonResult GetTreeView()<br>{<br>var result = GetAllNodes();<br>return Json(result, JsonRequestBehavior.AllowGet);<br>}<\/p>\n\n\n\n<p>\/\/Controller method with sample data<br>public List&lt;TreeNode&gt; GetAllNodes()<br>{<br>var list = new List&lt;TreeNode&gt;<br>{<br>new TreeNode{ id = 1, label = \u201cRoot\u201d ,checkbox=true, inode=true, radio=false, open=true },<br>new TreeNode{ id = 2, parent= 1, label = \u201cNode-1.1\u201d, checkbox=false, inode=true, radio=true, open=true },<br>new TreeNode{ id = 3, parent= 2, label = \u201cNode-1.1.1\u2033 , checkbox=false,inode=false, radio=false, open=true ,myurl=\u201dhttp:\/\/google.com\u201d},<br>new TreeNode{ id = 4, parent= 2, label = \u201cNode-1.1.2\u2033 , checkbox=false,inode=false, radio=false, open=true ,myurl=\u201dhttp:\/\/facebook.com\u201d},<br>new TreeNode{ id = 5, parent= 1, label = \u201cNode-1.2\u201d , checkbox=true,inode=true, radio=false, open=true },<br>new TreeNode{ id = 6, parent= 5, label = \u201cNode-1.2.1\u201d , checkbox=true,inode=false, radio=false, open=true },<br>new TreeNode{ id = 7, parent= 5, label = \u201cNode-1.2.2\u201d, checkbox=true,inode=false, radio=false, open=true },<br>new TreeNode{ id = 8, parent= 5, label = \u201cNode-1.2.3\u201d , checkbox=false,inode=true, radio=false, open=true },<br>new TreeNode{ id = 9, parent= 8, label = \u201cNode-1.2.3.1\u201d, checkbox=false,inode=true, radio=false, open=true },<br>new TreeNode{ id = 10, parent= 9, label = \u201cNode-1.2.3.1.1\u201d , checkbox=false,inode=false, radio=true, open=true }<br>};<\/p>\n\n\n\n<p>\/\/it\u2019s an extension method for filling the Treende class<\/p>\n\n\n\n<p>TreeNode tree = list.ToTree(); \/\/this method is for filling data in class<\/p>\n\n\n\n<p>List&lt;TreeNode&gt; lstTreeNode = new List&lt;TreeNode&gt;();<br>lstTreeNode.Add(tree);<\/p>\n\n\n\n<p>return lstTreeNode;<br>}<br>Extension class for filling treenode class (its optional you can implement on your own)<br>public static class TreeNodeExtensions<br>{<br>public static TreeNode ToTree(this List&lt;TreeNode&gt; list)<br>{<br>if (list == null) throw new ArgumentNullException(\u201clist\u201d);<br>var root = list.SingleOrDefault(x =&gt; x.parent == null);<br>if (root == null) throw new InvalidOperationException(\u201croot == null\u201d);<\/p>\n\n\n\n<p>PopulateChildren(root, list);<\/p>\n\n\n\n<p>return root;<br>}<\/p>\n\n\n\n<p>private static void PopulateChildren(TreeNode node, List&lt;TreeNode&gt; all)<br>{<br>var childs = all.Where(x =&gt; x.parent.Equals(node.id)).ToList();<\/p>\n\n\n\n<p>foreach (var item in childs)<br>{<br>node.open = true;<br>\/\/node.leaf = false;<br>node.branch.Add(item);<br>}<\/p>\n\n\n\n<p>foreach (var item in childs)<br>all.Remove(item);<\/p>\n\n\n\n<p>foreach (var item in childs)<br>PopulateChildren(item, all);<br>}<br>}<\/p>\n\n\n\n<p><strong>Sample output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"270\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/pawan-300x270-1.webp\" alt=\"\" class=\"wp-image-18765\"\/><\/figure>\n\n\n\n<p><strong>The code can be downloaded from&nbsp;<a href=\"https:\/\/winwireinc-my.sharepoint.com\/personal\/pawan_somani_winwire_com\/_layouts\/15\/guestaccess.aspx?guestaccesstoken=Ndh0qNkvR8TVVAtAuyxZKMJDeCk9dH%2b1rH%2f6%2fM55qJo%3d&amp;docid=1125d244e6d1e4aa895a394bc038ff520&amp;rev=1\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/strong><\/p>\n\n\n\n<p><strong><em>You can access the portal&nbsp;<a href=\"http:\/\/acoderinsights.ro\/en\/aciTree-tree-view-with-jQuery%20\" target=\"_blank\" rel=\"noopener\">here<\/a>&nbsp;for a deep dive into all of the areas mentioned above.<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the frequently used control is Tree view and Microsoft doesn\u2019t have this in the default set of controls. We have to depend on the third party\/JQuery controls. One of the free control is aciTree and can be download from Nuget. aciTree is a customizable JavaScript tree view control written as a jQuery plugin.&hellip; <a class=\"more-link\" href=\"https:\/\/viewmyprojects.com\/winwirewp\/blog\/how-to-create-tree-view-using-asp-net-mvc\/\">Continue reading <span class=\"screen-reader-text\">How to Create Tree view using ASP.NET MVC<\/span><\/a><\/p>\n","protected":false},"author":42,"featured_media":16793,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_eb_attr":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-11353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","entry"],"acf":[],"featured_image_src":"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Create-Treeusing-ASP.webp","author_info":{"display_name":"Pawan","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/pawan\/"},"views":4489,"uagb_featured_image_src":{"full":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Create-Treeusing-ASP.webp",800,440,false],"thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Create-Treeusing-ASP-150x150.webp",150,150,true],"medium":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Create-Treeusing-ASP-300x165.webp",300,165,true],"medium_large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Create-Treeusing-ASP-768x422.webp",750,412,true],"large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Create-Treeusing-ASP.webp",750,413,false],"1536x1536":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Create-Treeusing-ASP.webp",800,440,false],"2048x2048":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Create-Treeusing-ASP.webp",800,440,false],"post-thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Create-Treeusing-ASP.webp",800,440,false]},"uagb_author_info":{"display_name":"Pawan","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/pawan\/"},"uagb_comment_info":0,"uagb_excerpt":"One of the frequently used control is Tree view and Microsoft doesn\u2019t have this in the default set of controls. We have to depend on the third party\/JQuery controls. One of the free control is aciTree and can be download from Nuget. aciTree is a customizable JavaScript tree view control written as a jQuery plugin.&hellip;&hellip;","_links":{"self":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/11353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/comments?post=11353"}],"version-history":[{"count":3,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/11353\/revisions"}],"predecessor-version":[{"id":22604,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/11353\/revisions\/22604"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media\/16793"}],"wp:attachment":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media?parent=11353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/categories?post=11353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/tags?post=11353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}