【问题标题】:Left side nav bar on specific pages utilizing MVC model data for each page特定页面上的左侧导航栏利用每个页面的 MVC 模型数据
【发布时间】:2019-08-13 16:07:04
【问题描述】:

我已将代码用于左侧导航栏found here。但是,我只需要它出现在特定页面上。当它存在时,我需要将数据从 View 模型传递到导航栏中的链接,以便成功导航到所选的正确页面。

除了在每个页面上复制粘贴代码之外,还有其他最佳做法吗?似乎我可以将导航栏放在 _Layout.cshtml 上,让它出现在指定的页面上,然后使用 MVC 或 jQuery 将数据传递给链接。

【问题讨论】:

    标签: asp.net-core model-view-controller


    【解决方案1】:

    您可以查看 ASP.NET Core view components。在_Layout.cshtml 中,添加一些逻辑来确定是否要显示组件,然后使用适当的参数渲染它。

    这是一个示例,其中链接是根据从控制器传递的名称选择的,但您可以添加所需的任何逻辑,包括从数据库中获取它们。

    HomeController.cs

    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            // data to be passed to the component; 
           // will also be used to determine if the navbar should be displayed
            ViewData["NavMenuPage"] = "Index";
            return View();
        }
    }
    

    _Layout.cshtml

    // condition to render the navigation menu 
    @if (ViewData["NavMenuPage"] != null)
    {
        // ASP.NET Core will search for a component named NavMenu, 
        // and invoke it with the provided data (here, the NavMenuPage set in the controller)
        @await Component.InvokeAsync("NavMenu", ViewData["NavMenuPage"])
    }
    

    NavMenuViewComponent.cs

    这里,NavMenu 是您的组件的名称,并且类名必须以 ViewComponent 为后缀,以便 ASP.NET Core 找到它。该类可以放在项目中的任何位置。

    public class NavMenuViewComponent : ViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync(string pageName)
        {
            var links = GetLinksForPage(pageName);
            return View(links);
        }
    
        private Dictionary<string, string> GetLinksForPage(string pageName)
        {
            Dictionary<string, string> links = null;
            switch (pageName)
            {
                case "Index":
                    links = new Dictionary<string, string> {
                        { "Link 1", "https://www.google.com" },
                        { "Link 2", "https://www.example.com" },
                    };
    
                    break;
    
                case "Privacy":
                    links = new Dictionary<string, string> {
                        { "Link 3", "https://www.stackoverflow.com" },
                    };
    
                    break;
            }
    
    
            return links;
        }
    }
    

    /Views/Shared/Components/NavMenu/Default.cshtml

    这是呈现链接的代码。请注意,此文件不能放在任何地方:它必须遵循某些命名约定,如文档中所述。在这里,我把它放在共享文件夹中。

    // type of the object passed to View() call in NavMenuComponent.InvokeAsync
    @model Dictionary<string, string>
    
    <nav>
        @foreach (var link in Model)
        {
            <div><a href="@link.Value">@link.Key</a></div>
        }
    </nav>
    

    【讨论】:

    • 太棒了!我将查看这些文档并了解如何将其最好地集成到我的项目中。
    • 我没有使用 ViewCompnents,但使用 ViewData 来确定哪个页面获取哪个导航菜单有很大帮助!为你点赞,我的朋友!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多