Dynamic menu from the database

I have Login pagewhere, when logging in Home Page, the user is logged in , where is logged in dynamic menu. The problem is that when a user clicks on one menulink, loaded menudoes not appear

This is because I wrote the code inside Index action Home controller.

So my question is where should I write logic for dynamic menuso that it is available when the menulink button is clicked.

_Layout.cshtml file where the menu is loaded

 @model SMS.Models.ViewModel.DashboardVM

 @if (Model != null && Model.MenuParentList.Count > 0)
            {
                <!-- Sidebar Menu -->
                <ul class="sidebar-menu">
                    <li class="header">MAIN NAVIGATION</li>
                    <li class="active">
                        <a href="#">
                            <i class="fa fa-dashboard"></i> <span>Dashboard</span>
                        </a>
                    </li>
                    @foreach (var parentItem in Model.MenuParentList)
                    {
                        <li class="treeview">
                            <a href="#">
                                <i class="fa fa-th"></i>
                                <span>@parentItem.MenuParentName</span>
                                <i class="fa fa-angle-left pull-right"></i>
                            </a>
                            <ul class="treeview-menu">
                                @Html.Partial("_MenuParent", Model.MenuList.Where(x => x.ParentID == parentItem.MenuParentID))
                            </ul>
                        </li>
                    }                       

                </ul> 
            }      

The logic for the dynamic menu goes here

 public ActionResult Index()
    {


            var _dashboardVM = new DashboardVM
            {
                User = _employee.Users.FirstOrDefault(),

                MenuParentList=_db.Menus
                                 .Where(x => _parentList.Contains(x.Id))
                                 .Select(x => new SMS.Models.ViewModel.DashboardVM.MenuParent
                                 {
                                     MenuParentID = x.Id,
                                     MenuParentName = x.MenuName
                                 })
                                 .OrderBy(x=>x.MenuParentID)
                                 .ToList(),
                MenuList=_employee.Designation.Role.MenuRoles
                                              .Select(x=>x.Menu)
                                              .ToList()

            };
     }
0
source share
1 answer

Create a separate method [ChildActionOnly]that generates your menu and calls it from the layout page so that it is accessible on all pages

[ChildActionOnly]
public ActionResult Menu()
{
  var model = new DashboardVM
  {
     ....
  }
  return PartialView("_Menu", model);
}

_Menu.cshtml html

@model DashboardVM
....

, @model SMS.Models.ViewModel.DashboardVM ( , , ),

@Html.Action("Menu", yourControllerName)

Menu , .

+3

Source: https://habr.com/ru/post/1614560/


All Articles