简体   繁体   English

在_Layout Page中使用PartialView创建动态MainMenu - Asp.net core razorpage

[英]Using PartialView to create dynamic MainMenu in _Layout Page - Asp.net core razorpage

i want to using PartialView in My _Layout Page and this is my PartialView code:我想在我的 _Layout 页面中使用 PartialView,这是我的 PartialView 代码:

  @model List<WebApplication4.Models.MainMenuTable>

@foreach (var item in Model)
{
    <li class="nav-item">
        <a class="nav-link text-dark" asp-page="@item.Link">@item.Title</a>
    </li>
}

and this is my _Layout Page code:这是我的 _Layout 页面代码:

@{
    var partialModel = new List<MainMenuTable>()
    {
   
    };
}

and:和:

<partial name="_MainMenu" model="partialModel"/>

but i saw nothing in result why?但我什么也没看到结果为什么?

Your code is OK, But you need to pass some data into Partial View when you use it in _Layout.cshtml , Refer to this simple demo:你的代码没问题,但是在_Layout.cshtml中使用的时候需要传入一些数据到Partial View中,参考这个简单的demo:

@{
        var partialModel = new List<MainMenuTable>()
        {
            new MainMenuTable
            {
                Title = "HomePage",
                Link = "index"
            },
            new MainMenuTable
            {
                Title = "PrivacyPage",
                Link = "Privacy"
            }
        };
    }

    <partial name="_MainMenu" model="partialModel"/>

在此处输入图像描述

When you don't pass any data into Partival View , It will not show in your _Layout .当您不将任何数据传递到Partival View时,它不会显示在您的_Layout中。

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

相关问题 如何在 asp.net core razorpage 的布局页面中接收 cookie? - How can I receive cookie in layout page in asp.net core razorpage? Visual Studio 2017中新ASP.NET Core Razorpage的模板 - Template for new ASP.NET Core Razorpage in Visual Studio 2017 无法将JSON发布到ASP.NET Core RazorPage处理程序 - Cannot post JSON to an ASP.NET Core RazorPage handler 在asp.net核心razorpage中禁用特定按钮的按钮验证 - disable button validation in asp.net core razorpage for specific button ASP.Net Core 中的异步 PartialView - Async PartialView in ASP.Net Core 尝试使用 asp.net core 2.0 创建一个 excel 页面 - Trying to create an excel page using asp.net core 2.0 为什么 PartialView 模型在 PostBack 到 Asp.Net Core Razor 主页视图的控制器中为空? - Why are the PartialView models null in a PostBack to an Asp.Net Core Razor main page view's controller? Asp.net Razorpage:复选框上的事件处理程序未触发 - Asp.net Razorpage: Eventhandler on Checkbox is not firing 节目 <select> PartialView ASP.NET Core中的值 - Show <select> values in PartialView ASP.NET Core 如何使用 ASP.NET Core 3 中的 Ajax 函数将 PartialView 插入到普通视图中? - How do I insert a PartialView into a normal View using an Ajax-Function in ASP.NET Core 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM