简体   繁体   English

在 _Header 部分中,如何从 db 查询到呈现菜单?

[英]In _Header partial, how to query from db to render menu?

Layout:布局:

<partial name="_HeaderPartial" />
<div class="container">
    <main role="main" class="pb-3">
        @RenderBody()
    </main>
</div>
<partial name="_FooterPartial" />

_Header: _标题:

<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
    <div class="container">
        <a class="navbar-brand" asp-controller="Home" asp-action="Index">Admin</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
            <ul class="navbar-nav flex-grow-1">
                <li class="nav-item">
                    <a class="nav-link text-dark" asp-controller="Home" asp-action="Index">Home</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

Now I need to replace nav-item , each page route and name comes from databse.现在我需要替换nav-item ,每个页面路由和名称都来自数据库。 Since there is not a particular corresponded controller for header partial.由于 header 部分没有特定对应的 controller。

So do I have to add ViewBag in each controller?那么我必须在每个 controller 中添加 ViewBag 吗? like this:像这样:

    public IActionResult Index() // page1
    {
        ViewBag.menus = _svc.getMenu(); 
        return View();
    }

    public IActionResult Thanks() // page2
    {
        ViewBag.menus = _svc.getMenu(); 
        return View();
    }

_Header _标题

                @foreach (var menmu in ViewBag.menus)
                {
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-controller="@menmu.controller" asp-action="@menmu.action">@menmu.title</a>
                    </li>
                }

I mean I konw its not right to do so, but I don't know what is the best way to figure it out.我的意思是我知道这样做是不对的,但我不知道什么是最好的解决方法。

I have done this as follows (in an MVC5 app, it looks like you are using Razor Pages so for you it may be different, but I think it can still help you)我已按如下方式执行此操作(在 MVC5 应用程序中,您似乎正在使用 Razor 页面,因此对您而言可能有所不同,但我认为它仍然可以帮助您)

  1. Create a menu partial view, with it's own Menu model class.创建一个菜单局部视图,使用它自己的菜单 model class。 Place the markup for your menu there将菜单的标记放在那里
  2. Add a Menu() method to your HomeController (or some other controller, doesn't really matter), that returns a PartialViewResult.将 Menu() 方法添加到您的 HomeController(或其他一些 controller,并不重要),返回 PartialViewResult。 I've marked it as [ChildActionOnly] to make sure it can only be used as partial view.我已将其标记为[ChildActionOnly]以确保它只能用作部分视图。 I also added [OutPutCache(Duration = 60)] This method will get the data, create the model and return a call to PartialView("_Menu, menuModel) (or whatever you named the partial view and model)我还添加了[OutPutCache(Duration = 60)]此方法将获取数据,创建 model 并返回对PartialView("_Menu, menuModel)的调用(或您命名的部分视图和模型)
  3. In your header, add @Html.Action("Menu", "Home") to render the menu在您的 header 中,添加@Html.Action("Menu", "Home")以呈现菜单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM