Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在mvc布局页面中显示动态菜单_C#_Sql Server_Asp.net Mvc_Razor - Fatal编程技术网

C# 在mvc布局页面中显示动态菜单

C# 在mvc布局页面中显示动态菜单,c#,sql-server,asp.net-mvc,razor,C#,Sql Server,Asp.net Mvc,Razor,我想在mvc布局页面中显示动态菜单。 在我的数据库表中,有menuid和parentid,我想从中显示嵌套菜单。 如果有人有解决方案,请帮助我,如果有任何其他方法,请给出一个例子。 这是我的数据库 这是我的控制器代码 public ActionResult Index() { using (MachineShopDBEntities db = new MachineShopDBEntities()) { List<MenuMa

我想在mvc布局页面中显示动态菜单。 在我的数据库表中,有menuid和parentid,我想从中显示嵌套菜单。 如果有人有解决方案,请帮助我,如果有任何其他方法,请给出一个例子。 这是我的数据库
这是我的控制器代码

public ActionResult Index()
    {
        using (MachineShopDBEntities db = new MachineShopDBEntities())
        {
            List<MenuMaster> list = db.MenuMasters.ToList();
            ViewBag.MenuList = new SelectList(list);
        }
        return View();
    }
public ActionResult Index()
{
使用(MachineShopDBEntities db=new MachineShopDBEntities())
{
List=db.MenuMasters.ToList();
ViewBag.MenuList=新建选择列表(列表);
}
返回视图();
}
这是我的模型

public partial class MenuMaster
{
    public int MenuID { get; set; }
    public string MenuText { get; set; }
    public string Description { get; set; }
    public Nullable<int> ParentID { get; set; }
    public string ControllerName { get; set; }
    public string ActionName { get; set; }

    public bool IsChecked { get; set; }
    public List<MenuMaster> menus { get; set; }
    public IEnumerable<SelectListItem> users { get; set; }
}
public分部类菜单主
{
public int MenuID{get;set;}
公共字符串MenuText{get;set;}
公共字符串说明{get;set;}
公共可为空的ParentID{get;set;}
公共字符串控制器名称{get;set;}
公共字符串ActionName{get;set;}
已检查公共布尔值{get;set;}
公共列表菜单{get;set;}
公共IEnumerable用户{get;set;}
}
这是我的看法

<ul class="sidebar-menu">
                @{
                    if (ViewBag.MenuList != null)
                    {
                        foreach (var items in ViewBag.MenuList.Items)
                        {
                            string action = items.ActionName;
                            string controller = items.ControllerName;
                            <li class="treeview">
                                @if (items.ParentID == items.MenuID)
                                {
                                    <ul class="treeview-menu">
                                        <li class="treeview">
                                            <a href="/@items.ControllerName/@items.ActionName">
                                                <i class="fa fa-angle-double-right"></i> <span>@items.MenuText</span>
                                                <i class="fa fa-angle-left pull-right"></i>
                                            </a>
                                        </li>
                                    </ul>
                                }

                                <a href="/@items.ControllerName/@items.ActionName">
                                    <i class="fa fa-user"></i> <span>@items.MenuText</span>
                                    <i class="fa fa-angle-left pull-right"></i>
                                </a>
                            </li>

                        }
                    }
                }
    @{ 如果(ViewBag.MenuList!=null) { foreach(ViewBag.MenuList.items中的变量项) { string action=items.ActionName; 字符串控制器=items.ControllerName;
  • @if(items.ParentID==items.MenuID) {
    }
  • } } }
我想要这种类型的输出

您需要一个即使有第n个子菜单也可以运行的one递归方法

1)树状视图侧边栏菜单

在razor视图中添加以下方法(_Layout.cshtml)

对于这个例子,我使用了测试数据,如

public List<MenuMaster> MyMenus()
{
    return new List<MenuMaster> {
    new MenuMaster { MenuId  =1, MenuText="Home", ParentId = 0, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =2, MenuText="Sales", ParentId = 0, ControllerName="Sales", ActionName = "Sales" },
    new MenuMaster { MenuId  =3, MenuText="Report", ParentId = 0, ControllerName="Report", ActionName = "Report" },
    new MenuMaster { MenuId  =4, MenuText="About Us", ParentId = 1, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =5, MenuText="Company Profile", ParentId = 1, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =6, MenuText="Add Invoice", ParentId = 2, ControllerName="Sale", ActionName = "Sale" },
    new MenuMaster { MenuId  =7, MenuText="Update Invice", ParentId = 2, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =8, MenuText="Delete Invoice", ParentId = 2, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =9, MenuText="Daily Report", ParentId = 3, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =10, MenuText="Monthly Report", ParentId = 3, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =11, MenuText="Update Invice 1", ParentId = 7, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =12, MenuText="Update Invice 2", ParentId = 11, ControllerName="Home", ActionName = "Index" },
    };
}
公共列表我的菜单()
{
返回新列表{
新MenuMaster{MenuId=1,MenuText=“Home”,ParentId=0,ControllerName=“Home”,ActionName=“Index”},
新MenuMaster{MenuId=2,MenuText=“Sales”,ParentId=0,ControllerName=“Sales”,ActionName=“Sales”},
新MenuMaster{MenuId=3,MenuText=“Report”,ParentId=0,ControllerName=“Report”,ActionName=“Report”},
新的MenuMaster{MenuId=4,MenuText=“关于我们”,ParentId=1,ControllerName=“Home”,ActionName=“Index”},
新的MenuMaster{MenuId=5,MenuText=“公司简介”,ParentId=1,ControllerName=“Home”,ActionName=“Index”},
新MenuMaster{MenuId=6,MenuText=“添加发票”,ParentId=2,ControllerName=“销售”,ActionName=“销售”},
新菜单主{MenuId=7,MenuText=“更新Invice”,ParentId=2,ControllerName=“Home”,ActionName=“Index”},
新菜单主{MenuId=8,MenuText=“删除发票”,ParentId=2,ControllerName=“主页”,ActionName=“索引”},
新MenuMaster{MenuId=9,MenuText=“每日报告”,ParentId=3,ControllerName=“主页”,ActionName=“索引”},
新菜单主{MenuId=10,MenuText=“月度报告”,ParentId=3,ControllerName=“Home”,ActionName=“Index”},
新菜单主{MenuId=11,MenuText=“更新版本1”,ParentId=7,ControllerName=“Home”,ActionName=“Index”},
新菜单主{MenuId=12,MenuText=“更新版本2”,ParentId=11,ControllerName=“Home”,ActionName=“Index”},
};
}
输出:


您需要一个即使有第n个子菜单也可以运行的one递归方法

1)树状视图侧边栏菜单

在razor视图中添加以下方法(_Layout.cshtml)

对于这个例子,我使用了测试数据,如

public List<MenuMaster> MyMenus()
{
    return new List<MenuMaster> {
    new MenuMaster { MenuId  =1, MenuText="Home", ParentId = 0, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =2, MenuText="Sales", ParentId = 0, ControllerName="Sales", ActionName = "Sales" },
    new MenuMaster { MenuId  =3, MenuText="Report", ParentId = 0, ControllerName="Report", ActionName = "Report" },
    new MenuMaster { MenuId  =4, MenuText="About Us", ParentId = 1, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =5, MenuText="Company Profile", ParentId = 1, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =6, MenuText="Add Invoice", ParentId = 2, ControllerName="Sale", ActionName = "Sale" },
    new MenuMaster { MenuId  =7, MenuText="Update Invice", ParentId = 2, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =8, MenuText="Delete Invoice", ParentId = 2, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =9, MenuText="Daily Report", ParentId = 3, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =10, MenuText="Monthly Report", ParentId = 3, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =11, MenuText="Update Invice 1", ParentId = 7, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =12, MenuText="Update Invice 2", ParentId = 11, ControllerName="Home", ActionName = "Index" },
    };
}
公共列表我的菜单()
{
返回新列表{
新MenuMaster{MenuId=1,MenuText=“Home”,ParentId=0,ControllerName=“Home”,ActionName=“Index”},
新MenuMaster{MenuId=2,MenuText=“Sales”,ParentId=0,ControllerName=“Sales”,ActionName=“Sales”},
新MenuMaster{MenuId=3,MenuText=“Report”,ParentId=0,ControllerName=“Report”,ActionName=“Report”},
新的MenuMaster{MenuId=4,MenuText=“关于我们”,ParentId=1,ControllerName=“Home”,ActionName=“Index”},
新的MenuMaster{MenuId=5,MenuText=“公司简介”,ParentId=1,ControllerName=“Home”,ActionName=“Index”},
新MenuMaster{MenuId=6,MenuText=“添加发票”,ParentId=2,ControllerName=“销售”,ActionName=“销售”},
新菜单主{MenuId=7,MenuText=“更新Invice”,ParentId=2,ControllerName=“Home”,ActionName=“Index”},
新菜单主{MenuId=8,MenuText=“删除发票”,ParentId=2,ControllerName=“主页”,ActionName=“索引”},
新MenuMaster{MenuId=9,MenuText=“每日报告”,ParentId=3,ControllerName=“主页”,ActionName=“索引”},
新菜单主{MenuId=10,MenuText=“月度报告”,ParentId=3,ControllerName=“Home”,ActionName=“Index”},
新菜单主{MenuId=11,MenuText=“更新版本1”,ParentId=7,ControllerName=“Home”,ActionName=“Index”},
新菜单主{MenuId=12,MenuText=“更新版本2”,ParentId=11,ControllerName=“Home”,ActionName=“Index”},
};
}
输出:


上述代码是否有任何问题或错误?或不按预期工作?输出无错误,但。如何显示子级和子级及其子级。?好的,让我为您准备答案好的。谢谢。你们想要下拉菜单还是树状视图之类的菜单?上面的代码给出了任何问题或错误吗?或不按预期工作?输出无错误,但。如何显示child和su
public ActionResult Index()
{
    using (MenuMaster db = new MenuMaster())
    {
        List<MenuMaster> list = db.MyMenus().ToList();
        Session["MenuList"] = list;
    }
    return View();
}
public List<MenuMaster> MyMenus()
{
    return new List<MenuMaster> {
    new MenuMaster { MenuId  =1, MenuText="Home", ParentId = 0, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =2, MenuText="Sales", ParentId = 0, ControllerName="Sales", ActionName = "Sales" },
    new MenuMaster { MenuId  =3, MenuText="Report", ParentId = 0, ControllerName="Report", ActionName = "Report" },
    new MenuMaster { MenuId  =4, MenuText="About Us", ParentId = 1, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =5, MenuText="Company Profile", ParentId = 1, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =6, MenuText="Add Invoice", ParentId = 2, ControllerName="Sale", ActionName = "Sale" },
    new MenuMaster { MenuId  =7, MenuText="Update Invice", ParentId = 2, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =8, MenuText="Delete Invoice", ParentId = 2, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =9, MenuText="Daily Report", ParentId = 3, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =10, MenuText="Monthly Report", ParentId = 3, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =11, MenuText="Update Invice 1", ParentId = 7, ControllerName="Home", ActionName = "Index" },
    new MenuMaster { MenuId  =12, MenuText="Update Invice 2", ParentId = 11, ControllerName="Home", ActionName = "Index" },
    };
}