Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Menu and Sub-Menu comes all in once

$
0
0

Hi All,

I am creating a dynamic menu using EF and MVC. Below is my database structure for Menu and Submenu. 

The issue is that I have all menu and sub menu in a single table.

And I have MenuModel Class

 public class MenuModel
    {
        public string MainMenuName { get; set; }
        public long MainMenuId { get; set; }
        public string SubMenuName { get; set; }
        public long SubMenuId { get; set; }
        public int MenuLevel { get; set; }
}

Below is my query 

List<MenuModel> _menus = db.AdminSection_GEN.Select(x => new MenuModel
                {
                    MainMenuId = x.ParentMenuId,
                    MainMenuName = x.Title,
                    SubMenuId = x.MenuID,
                    SubMenuName = x.Title
                    //MenuLevel = x.IntLevel

                }).ToList()
Session["MenuMaster"] = _menus;

(I think I need to modify my query and don't know where to )

and on .cshtml I am doing like below

var MenuMaster = (List<CP.Models.MenuModel>)Session["MenuMaster"];
        var groupByMenu = MenuMaster.GroupBy(x => x.MainMenuName).ToList();

        foreach (var MenuList in groupByMenu)
        {

            <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">@MenuList.Key<span class="caret"></span></a><ul class="dropdown-menu">
                    @foreach (var SubMenuList in MenuList)
                    {<li><a href="#">@SubMenuList.SubMenuName</a></li>
                    }</ul></li>

        }

But in out put all menu and sub-menu comes all along with not in hierarchical manner.

Please suggest.


Viewing all articles
Browse latest Browse all 1698

Trending Articles