Dynamic Navigation Menu with MVC (Using Telerik Kendo)


During my current assignment I am working a lot with Telerik Kendo UI controls for MVC. Using this we tried to give lots of flexibly to our product, We have almost all the section on pages are dynamic, i. e. they generate on the base of defined criteria and data fetched from database.

Below sample code demonstrate generation of dynamic Navigation/menu bar generating using kendo menu,



@using Weezer.Models
@model List<StoreProc_Result>

<div class="row">

   @(Html.Kendo().Menu()
  .Name("menu")
  .Items(items =>
  {
    foreach (var Menu in Model.Where(m => m.MenuLevel == 0))
     {
      if (Menu.MenuLevel == 0)
       {
         items.Add()
         .Text(Menu.MenuName).HtmlAttributes(new { style = "color:black;font-weight: normal;" }).Action(Menu.ActionName, Menu.ControllerName)
         .Items(children =>
           {
            foreach (var ChildMenu in Model.Where(m => m.MenuLevel == Menu.MenuId))
             {
               children.Add().Text(ChildMenu.MenuName).HtmlAttributes(new { style = "color:black;font-weight: normal;" }).Action(ChildMenu.ActionName, ChildMenu.ControllerName);
             }
           });
       }
     }
  })
)

</div>


 

Here I have above code in my partial view, which is paced on Master/Layout page. This partial page can be used on page as,

@Html.Partial("_LeftNavigation", Model.MenuList)


To return this partial view, I need to pass list of navigation or menus to this view. This list will look like as displayed din below table. Here as coded for each there are menu item with menu level 0 (i.e. parent Menu) It will add that item to list and again in this loop there is one more loop to generate child Menus.


MenuID
MenuName
ActionName
ControllerName
MenuLevel
OrderNo
1
Home
Home
Home
0
1
2
My Info
MyInfo
MyInfo
0
2
3
Life Event
LifeEvents
LifeEvents
0
3
4
Learn
NULL
NULL
0
4
5
My Info
MyInfo
MyInfo
2
1
6
My Contact
MyContact
MyInfo
2
2
7
My Work
MyWork
MyInfo
2
3
12
Marriage
NULL
NULL
3
1
13
Birth
NULL
NULL
3
2
14
Adoption
NULL
NULL
3
3

 

And our menu then look like as,



 


Comments

  1. Dynamic Navigation Menu With MVC (Using Telerik Kendo) is posible in ASPX view

    ReplyDelete
  2. If you are going to post an article, please ensure it is complete. For example, you have included your view code and your menu partial view code, what about your controller methods where you actually populate the model

    ReplyDelete
    Replies
    1. Thanks for suggestion, But in this case your controller action will have your own logic and to display menu on view you just need to pass model/list similar to table mentioned in blog.

      In given example I am directly calling partial view from another view and passing model in it as,

      @Html.Partial("_LeftNavigation", Model.MenuList)


      Delete
  3. Hello Sir,
    I don't know if you are still active. The above article was very helpful. but I just had one issue with the above code. The sub menus are always open. Is it missing any code or the code was meant to be all menus expanded? Could you please throw some light on how the menu should be collapsible and open only on click of the parent menu item.
    Thanks and Regards,
    Deepak.

    ReplyDelete

Post a Comment