【发布时间】:2021-01-28 05:42:22
【问题描述】:
我在 asp net core 应用程序中有一个布局页面。我想将数据库中的动态菜单绑定到它,它显示在项目的所有页面上,也有各种控制器和模型。这是我的布局页面代码:-
@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>
}
}
这是我的控制器,用于在家庭控制器中获取菜单项:-
public ActionResult _Menu()
{
ViewBag.Industries = _context.MasterIndustry.Include(u => u.MasterProductCategory).ToList();
return PartialView("_Menu", ViewBag.Industries);
}
问题是,在调试解决方案时,它正在从布局页面移动到部分页面,但它没有调用菜单控制器。结果 viewBag.Industry 为空。我必须在哪里编写菜单控制器,以便它调用菜单控制器的每个页面和每个控制器以获得所需的结果。或者如果有更好的方法来解决这个问题。请告诉我。
【问题讨论】:
标签: asp.net-core asp.net-core-mvc