从数据库的动态菜单 [英] Dynamic menu from database

查看:171
本文介绍了从数据库的动态菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录页其中,在登录时需要用户首页,其中动态菜单是loaded.The问题是,当一个用户点击 menulink 加载菜单是不可见的。

这是因为我已经写了code Index操作里面的 Home控制器

所以我的问题是我应该写动态菜单中的逻辑,以便它在点击menulink访问。

其中菜单加载_Layout.cshtml文件

  @model SMS.Models.ViewModel.DashboardVM @if(型号= NULL&放大器;!&安培; Model.MenuParentList.Count大于0)
            {
                <! - 工具栏菜单 - >
                < UL类=边栏菜单>
                    <李班=头>主导航< /李>
                    <李班=主动>
                        &所述; A HREF =#>
                            < I类=发发的仪表板>< I&GT /; <跨度>仪表及LT; / SPAN>
                        &所述; / A>
                    < /李>
                    @foreach(在Model.MenuParentList VAR parentItem)
                    {
                        <李班=树状>
                            &所述; A HREF =#>
                                < I类=发发个>< / I>
                                <跨度> @ parentItem.MenuParentName< / SPAN>
                                < I类=发发角左右拉式>< / I>
                            &所述; / A>
                            < UL类=树状菜单>
                                @ Html.Partial(_ MenuParent,Model.MenuList.Where(X => x.ParentID == parentItem.MenuParentID))
                            < / UL>
                        < /李>
                    }                < / UL>
            }

动态菜单逻辑放在这里

 公众的ActionResult指数()
    {
            VAR _dashboardVM =新DashboardVM
            {
                用户= _employee.Users.FirstOrDefault(),                MenuParentList = _db.Menus
                                 。凡(X => _parentList.Contains(x.Id))
                                 。选择(X =>新建SMS.Models.ViewModel.DashboardVM.MenuParent
                                 {
                                     MenuParentID = x.Id,
                                     MenuParentName = x.MenuName
                                 })
                                 .OrderBy(X => x.MenuParentID)
                                 .ToList(),
                菜单列表= _employee.Designation.Role.MenuRoles
                                              。选择(X => x.Menu)
                                              .ToList()            };
     }


解决方案

创建一个单独的 [ChildActionOnly] 方法生成您的菜单,并从页面布局叫它所以其在所有页面可用

  [ChildActionOnly]
公众的ActionResult菜单()
{
  VAR模型=新DashboardVM
  {
     ....
  }
  返回PartialView(_菜单,模型);
}

和创建一个 _Menu.cshtml 局部视图生成HTML

  @model DashboardVM
....

,然后在你的布局,删除 @model SMS.Models.ViewModel.DashboardVM (布局不应该有一个模型,除非该模式是所有型号的基类由布局使用),然后包括

  @ Html.Action(菜单,yourControllerName)

这将调用菜单方法,并将其插入到返回布局的局部视图。

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

            };
     }

解决方案

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.

这篇关于从数据库的动态菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆