This is a migrated thread and some comments may be shown as answers.

[Solved] Databinding menu against datatable

1 Answer 106 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
jörgen
Top achievements
Rank 1
jörgen asked on 16 Feb 2011, 11:40 PM
My Project is in VB.

I have a table Menus in the database with the following columns:
TennantId  varchar(4)
MenuId       int
MenuName  varchar(30)
ParentId     int
Controller  varchar(50)
Action       varchar(50)

From this table I select rows where TennantId = 'xxx' and get a number of rows (IE a List(of Menus))

I want to databind the menu control against this collection.

If ParentId = 0 this menuitem should be a top level menuitem.
If ParentId > 0 this menuitem should be a subitem to to the menuitem that has that value in MenuId. 

The menu should be placed on the master page and can have a depth between 1 and 3 levels.

Can anyone give examples on how to accomplish this. What needs to be in the controller action and what needs to be in the view?

1 Answer, 1 is accepted

Sort by
0
jörgen
Top achievements
Rank 1
answered on 17 Feb 2011, 04:49 PM
I went to the bathouse and sat in the jacuzzi and contemplated when the solution hit me. When I got home I made a testproject and I worked so I thought I share my solution with you all.

Make a reposityory class for the database table MenusRepository. It should have two methods:
Shareable Function HasChildren(TennantId, MenuId) as Boolean
Shareable Function GetMenus(TennantId, MenuId) as List(Of Menus)

In the ControllerAction you need to set what menu should be shown:
ViewData("TennantId") = "001"
' Let the action do its thing to display things below the menu.

In the View or in the Master page you paste the following code:

  <%: Html.Telerik().Menu() _
             .Name("MainMenu") _
             .Items(Sub(menu)
                      For Each smi As TelerikMvc3Test.Menus In TelerikMvc3Test.MenusRepository.GetMenus(ViewData("TennantId"), 0)
                        If TelerikMvc3Test.MenusRepository.HasChildren(ViewData("TennantId"), smi.MenuId) Then
                          ' MenuItem has ChildItems
                          Dim id2 As Integer = smi.MenuId
                          menu.Add().Text(smi.MenuName).Items(Sub(menu2)
                                                                For Each smi2 As TelerikMvc3Test.Menus In TelerikMvc3Test.MenusRepository.GetMenus(ViewData("TennantId"), id2)
                                                                  If TelerikMvc3Test.MenusRepository.HasChildren(ViewData("TennantId"), smi2.MenuId) Then
                                                                    ' MenuItem has ChildItems
                                                                    Dim id3 As Integer = smi2.MenuId
                                                                    menu2.Add().Text(smi2.MenuName).Items(Sub(menu3)
                                                                                                            ' Third level MenuItems is not allowed to have children.
                                                                                                            For Each smi3 As TelerikMvc3Test.Menus In TelerikMvc3Test.MenusRepository.GetMenus(ViewData("TennantId"), id2)
                                                                                                              menu3.Add().Text(smi3.MenuName).Action(smi3.Action, smi3.Controller)
                                                                                                            Next
                                                                                                          End Sub)
                                                                  Else
                                                                    ' MenuItem has no ChildItems
                                                                    menu2.Add.Text(smi2.MenuName).Action(smi2.Action, smi2.Controller)
                                                                  End If
                                                                Next
                                                              End Sub)
                        Else
                          ' MenuItem has no ChildItems
                          menu.Add().Text(smi.MenuName).Action(smi.Action, smi.Controller)
                        End If
                      Next
                    End Sub)
 %>
and there you have a menu system with up to three levels that is read from a database table.

The above example is written in Visual Basic; if you are a C# you can use Teleriks Converter to translate the code from VB to C# (http://converter.telerik.com).

I hope someone else will find this code useful.

Tags
Menu
Asked by
jörgen
Top achievements
Rank 1
Answers by
jörgen
Top achievements
Rank 1
Share this question
or