Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

asp.net mvc 4 - Dynamic menu from database

I have a Login page where on login it takes user to Home Page where dynamic menu is loaded.The problem is that when a user clicks on one of the menulink the loaded menu is not visible

This is because I have written the code inside Index action of theHome controller.

So my question is where should I write the logic for dynamic menu so that it is accessible on clicking the menulink.

_Layout.cshtml file where 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> 
            }      

Logic for 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()

            };
     }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Create a separate [ChildActionOnly] method that generates your menu and call it from the layout page so its available in all pages

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

and create a _Menu.cshtml partial view to generate the html

@model DashboardVM
....

and then in your layout, remove @model SMS.Models.ViewModel.DashboardVM (a layout should not have a model unless that model is a base class for all models used by the layout) and then include

@Html.Action("Menu", yourControllerName)

which will call the Menu method and insert the partial view it returns into the layout.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...