0

I'm working in Asp.Net MVC, and I want to populate menu from database, but I don't really have an idea how can I do it to insert sub-menus into each menu, and each of this sub-menu can have another submenu, etc.

I was is something similar like facebook comment and reply model.

First I have model of menu like:

public int MenuId { get; set; }
      public string Name { get; set; }
      public string Controller { get; set; }
      public string Action { get; set; }
      public int Order { get; set; }
      public string Icon { get; set; }
      public bool IsAdmin { get; set; } 

Sub-menu:

public int SubMenuId { get; set; }
      public int MenuId { get; set; } // Menu Fk
      public string Name { get; set; }
      public string Controller { get; set; }
      public string Action { get; set; }
      public int Order { get; set; }
      public string Icon { get; set; }
      public bool IsAdmin { get; set; } 

It's my logic correctly?, someone implement something similar? What I need to do into controller to make it works?, if someone have an example I really apreciated it. Thanks in Advance

5
  • Why not just have a Menu Object that contains a Nullable ParentId? Commented Dec 8, 2016 at 17:39
  • Can you explain me it? I only know basics of .net @Dobbins
    – Carl
    Commented Dec 8, 2016 at 17:40
  • Correct. That is why you have a Nullable field called ParentId. If Parentid is null you know its a top level menu Item. Then you can walk the tree building child items where parentId is equal to your Current Menu Id and so on. Hold on and Ill give you an example Commented Dec 8, 2016 at 17:43
  • thecrazyprogrammer.com/2015/02/… Pretty simple example Commented Dec 8, 2016 at 17:44
  • Thanks, I'm a little confused @Dobbins
    – Carl
    Commented Dec 8, 2016 at 17:45

2 Answers 2

0

The traditional way is to just have your Menu model and have a nullable ParentMenuId. If the ParentMenuId is null, your Menu is a root node. You can set your navigation properties so from each Menu node you can access .Children to get all of the nodes that point to it.

1
  • Can you explain me and example of how it works in controller please?
    – Carl
    Commented Dec 8, 2016 at 17:44
0

I believe @Dobbins has the correct idea. In the database you can have a MenuLink table:

MenuLink
- ID int IDENTITY(1,1) NOT NULL <- Primary Key
- ParentId int NULL <- Foreign Key
- OrderNumber int
- LinkUri varchar(1024) <- Can be replaced with Controller, Action 
- IconUri varchar(1024)
- Text nvarchar(256)

You can then select all menu links and their sub-links from the database.

Your Menu Object can then have a list of Menu Objects:

public class MenuLink 
{
   int Id { get; set; }
   int ParentId { get; set; }
   int OrderNumber { get; set; }
   Uri LinkUri { get; set; } 
   Uri IconUri { get; set; }
   string Text { get; set; }
   IList<MenuLink> SubMenuItems { get; set; }
}

Now you can use razor, or your JavaScript framework of choice to create the dynamic menu in code.

2
  • So ParentId is a foreignkey of Id?
    – Carl
    Commented Dec 8, 2016 at 18:16
  • @Carl - That's correct. ParentId is a Foreign Key to Id. Commented Oct 26, 2017 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.