[英]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.