Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Asp.net core 将动态菜单从数据库绑定到布局页面,并为asp net核心的所有页面显示它_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

Asp.net core 将动态菜单从数据库绑定到布局页面,并为asp net核心的所有页面显示它

Asp.net core 将动态菜单从数据库绑定到布局页面,并为asp net核心的所有页面显示它,asp.net-core,asp.net-core-mvc,Asp.net Core,Asp.net Core Mvc,我在asp net核心应用程序中有一个布局页面。我想将数据库中的动态菜单绑定到该页面,该页面显示在项目的所有页面上,并且具有各种控制器和模型。以下是我的布局页面代码:- @Html.Partial("_Menu", Model) 这是我名为_Menu的部分查看页面:- @if (ViewBag.Industries != null) { @foreach (var items in ViewBag.Industries) { <li class="lev

我在asp net核心应用程序中有一个布局页面。我想将数据库中的动态菜单绑定到该页面,该页面显示在项目的所有页面上,并且具有各种控制器和模型。以下是我的布局页面代码:-

 @Html.Partial("_Menu", Model)
这是我名为_Menu的部分查看页面:-

@if (ViewBag.Industries != null)
{
    @foreach (var items in ViewBag.Industries)
    {
        <li class="level0 parent drop-menu">
            <a href="index.html"><span>@items.IndustryName</span></a>
            <ul class="level1">
                @foreach (var subitems in items.MasterProductCategory)
                {
                    <li class="level1 first parent"><a href="404error.html"><span>@subitems.CategoryName</span></a></li>
                }
            </ul>
        </li>
    }
}

问题是,在调试解决方案时,它正在从布局页面移动到部分页面,但它没有调用菜单控制器。并给出viewBag.Industry为空的结果。我必须编写菜单控制器,以便为每个页面和每个控制器调用菜单控制器以获得所需的结果。或者如果有更好的方法来解决这个问题。请告诉我。

不要使用自动控制器。使用并调用布局

需要全球化每个控制器上的菜单,并在任何操作之前强制执行

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    // Your logic here...
    BindMenuItems();
    base.OnActionExecuting(filterContext);
}

public IActionResult BindMenuItems()
{
    ViewBag.Industries = _context.MasterIndustry.Include(u => u.MasterProductCategory).ToList();
    return View();
}
布局页面:

@if(ViewBag.Industries != null)
{                       
    @foreach(var items in ViewBag.Industries)
    { 
        <li class="level0 parent drop-menu">
            <a href="index.html"><span>@items.IndustryName</span></a>
            <ul class="level1">                                
                @foreach(var subitems in items.MasterProductCategory)
                { 
                    <li class="level1 first parent"><a href="404error.html"><span>@subitems.CategoryName</span></a></li>
                }                                  
            </ul>
        </li>
    }
}
@if(ViewBag.Industries!=null)
{                       
@foreach(ViewBag.Industries中的var项目)
{ 
    • @foreach(items.MasterProductCategory中的var子项) {
    • }
  • } }
    希望这能帮助其他面临同样问题的人。我不知道它是否是业务标准,但它帮助我立即获得解决方案

    @if(ViewBag.Industries != null)
    {                       
        @foreach(var items in ViewBag.Industries)
        { 
            <li class="level0 parent drop-menu">
                <a href="index.html"><span>@items.IndustryName</span></a>
                <ul class="level1">                                
                    @foreach(var subitems in items.MasterProductCategory)
                    { 
                        <li class="level1 first parent"><a href="404error.html"><span>@subitems.CategoryName</span></a></li>
                    }                                  
                </ul>
            </li>
        }
    }