Sunday, August 11, 2019

How to Populate Treeview Nodes Dynamically (On Demand) using in MVC 4 application

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok. 
In this example, I have used one tables as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >

Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish. 

Step-5: Add a new Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Step-6: Add new action into your controller for Get Method

Here I have added "OnDemand" Action into "Treeview" Controller. Please write this following code 

  1. public ActionResult OnDemand()
  2. {
  3. List<SiteMenu> all = new List<SiteMenu>();
  4. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  5. {
  6. all = dc.SiteMenus.Where(a => a.ParentMenuID.Equals(0)).ToList();
  7. }
  8. return View(all);
  9. }

Step-7:: Add another new action into your controller for Get Method for Get Sub Menu of selected menu.

Here I have added "GetSubMenu" Action into "Treeview" Controller. Please write this following code 

  1. public JsonResult GetSubMenu(string pid)
  2. {
  3. // this action for Get Sub Menus from database and return as json data
  4. System.Threading.Thread.Sleep(5000);
  5. List<SiteMenu> subMenus = new List<SiteMenu>();
  6. int pID = 0;
  7. int.TryParse(pid, out pID);
  8. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  9. {
  10. subMenus = dc.SiteMenus.Where(a => a.ParentMenuID.Equals(pID)).OrderBy(a => a.MenuName).ToList();
  11. }
  12.  
  13. return new JsonResult { Data= subMenus, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  14. }

Step-8: Add js file for get submenu and make this treeview collapsiable.

Go to Solution Explorer > Right Click on Script folder > ADD > New Item > Select Javascript file under Web > Enter File name > add. 
  1. $(document).ready(function () {
  2. $(".collapsible").live("click", function (e) {
  3. e.preventDefault();
  4.  
  5. var this1 = $(this); // Get Click item
  6. var data = {
  7. pid: $(this).attr('pid')
  8. };
  9. var isLoaded = $(this1).attr('data-loaded'); // Check data already loaded or not
  10. if (isLoaded == "false") {
  11. $(this1).addClass("loadingP"); // Show loading panel
  12. $(this1).removeClass("collapse");
  13.  
  14. // Now Load Data Here
  15. $.ajax({
  16. url: "/Treeview/GetSubMenu",
  17. type: "GET",
  18. data: data,
  19. dataType: "json",
  20. success: function (d) {
  21. $(this1).removeClass("loadingP");
  22.  
  23. if (d.length > 0) {
  24.  
  25. var $ul = $("<ul></ul>");
  26. $.each(d, function (i, ele) {
  27. $ul.append(
  28. $("<li></li>").append(
  29. "<span class='collapse collapsible' data-loaded='false' pid='"+ele.MenuID+"'>&nbsp;</span>" +
  30. "<span><a href='"+ele.NavURL+"'>"+ele.MenuName+"</a></span>"
  31. )
  32. )
  33. });
  34.  
  35. $(this1).parent().append($ul);
  36. $(this1).addClass('collapse');
  37. $(this1).toggleClass('collapse expand');
  38. $(this1).closest('li').children('ul').slideDown();
  39. }
  40. else {
  41. // no sub menu
  42. $(this1).css({'dispaly':'inline-block','width':'15px'});
  43. }
  44.  
  45. $(this1).attr('data-loaded', true);
  46. },
  47. error: function () {
  48. alert("Error!");
  49. }
  50. });
  51. }
  52. else {
  53. // if already data loaded
  54. $(this1).toggleClass("collapse expand");
  55. $(this1).closest('li').children('ul').slideToggle();
  56. }
  57.  
  58. });
  59. });

Step-9: Add view for the Action (OnDemand) & design.

Right Click on Action Method (here right click on form action) > Add View... > Enter View Name > Select View Engine (Razor) > Check "Create a strong-typed view" > Select your model class > Add.[N:B:Please Rebuild solution before add view.] 
HTML Code 
  1. @model List<MVATreeviewOndemand.SiteMenu>
  2. @{
  3. ViewBag.Title = "OnDemand";
  4. }
  5. @* Here We will add some css for looks treeview good *@
  6.  
  7. <h2>OnDemand - Treeview</h2>
  8. <div style="border:1px solid black; padding:0px; background-color:#FAFAFA">
  9. @{
  10. <div class="treeview">
  11. @{
  12. if (Model != null && Model.Count() > 0)
  13. {
  14. @*Here I will going to load Treeview menus*@
  15. <ul>
  16. @foreach (var i in Model)
  17. {
  18. <li>
  19. <span class="collapse collapsible" data-loaded="false" pid="@i.MenuID">&nbsp;</span>
  20. @* Here I have added the above span for collapsible button for treeview *@
  21. @* and data-loaded="false" means its sub menu not loaded yet from database *@
  22. <span>
  23. <a href="@i.NavURL">@i.MenuName</a>
  24. </span>
  25. </li>
  26. }
  27. </ul>
  28. }
  29. }
  30. </div>
  31. }
  32. </div>
CSS Code 
  1. <style>
  2. .loadingP {
  3. background-image: url('../Images/generated-image.gif');
  4. width: 15px;
  5. display: inline-block;
  6. }
  7.  
  8. .collapse {
  9. width: 15px;
  10. background-image: url('../Images/ui-icons_454545_256x240.png');
  11. background-repeat: no-repeat;
  12. background-position: -36px -17px;
  13. display: inline-block;
  14. cursor: pointer;
  15. }
  16.  
  17. .expand {
  18. width: 15px;
  19. background-image: url('../Images/ui-icons_454545_256x240.png');
  20. background-repeat: no-repeat;
  21. background-position: -50px -17px;
  22. display: inline-block;
  23. cursor: pointer;
  24. }
  25.  
  26. .treeview ul {
  27. font: 14px Arial, Sans-Serif;
  28. margin: 0px;
  29. padding-left: 20px;
  30. list-style: none;
  31. }
  32.  
  33. .treeview > li > a {
  34. font-weight: bold;
  35. }
  36.  
  37. .treeview li a {
  38. padding: 4px;
  39. font-size: 12px;
  40. display: inline-block;
  41. text-decoration: none;
  42. width: auto;
  43. }
  44. </style>
JS Code 
  1. @section Scripts{
  2. @* Here I am going to add js code for populate submenu for the selected tree node *@
  3. <script src="~/Scripts/MyTreeview.js"></script>
  4. }
Complete View 
  1. @model List<MVATreeviewOndemand.SiteMenu>
  2. @{
  3. ViewBag.Title = "OnDemand";
  4. }
  5. @* Here We will add some css for looks treeview good *@
  6.  
  7. <style>
  8. .loadingP {
  9. background-image: url('../Images/generated-image.gif');
  10. width: 15px;
  11. display: inline-block;
  12. }
  13.  
  14. .collapse {
  15. width: 15px;
  16. background-image: url('../Images/ui-icons_454545_256x240.png');
  17. background-repeat: no-repeat;
  18. background-position: -36px -17px;
  19. display: inline-block;
  20. cursor: pointer;
  21. }
  22.  
  23. .expand {
  24. width: 15px;
  25. background-image: url('../Images/ui-icons_454545_256x240.png');
  26. background-repeat: no-repeat;
  27. background-position: -50px -17px;
  28. display: inline-block;
  29. cursor: pointer;
  30. }
  31.  
  32. .treeview ul {
  33. font: 14px Arial, Sans-Serif;
  34. margin: 0px;
  35. padding-left: 20px;
  36. list-style: none;
  37. }
  38.  
  39. .treeview > li > a {
  40. font-weight: bold;
  41. }
  42.  
  43. .treeview li a {
  44. padding: 4px;
  45. font-size: 12px;
  46. display: inline-block;
  47. text-decoration: none;
  48. width: auto;
  49. }
  50. </style>
  51.  
  52. <h2>OnDemand - Treeview</h2>
  53. <div style="border:1px solid black; padding:0px; background-color:#FAFAFA">
  54. @{
  55. <div class="treeview">
  56. @{
  57. if (Model != null && Model.Count() > 0)
  58. {
  59. @*Here I will going to load Treeview menus*@
  60. <ul>
  61. @foreach (var i in Model)
  62. {
  63. <li>
  64. <span class="collapse collapsible" data-loaded="false" pid="@i.MenuID">&nbsp;</span>
  65. @* Here I have added the above span for collapsible button for treeview *@
  66. @* and data-loaded="false" means its sub menu not loaded yet from database *@
  67. <span>
  68. <a href="@i.NavURL">@i.MenuName</a>
  69. </span>
  70. </li>
  71. }
  72. </ul>
  73. }
  74. }
  75. </div>
  76. }
  77. </div>
  78.  
  79. @section Scripts{
  80. @* Here I am going to add js code for populate submenu for the selected tree node *@
  81. <script src="~/Scripts/MyTreeview.js"></script>
  82. }
  83.  
Step-10: Run Application

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...