布局.cshtml
@{
var menus = (IEnumerable<WebApplication.Models.TopMenu>)ViewBag.menus;
}
<div class="col-md-8">
<!-- Menu Partial -->
@await Html.PartialAsync("~/Views/MainPage/TopMenu.cshtml", menus);
</div>
MainPageController.cs
public IActionResult Mainpage()
{
ViewBag.menus = db.TopMenu.Where(a => a.Active).OrderBy(a => a.Order);
return View();
}
TopMenu.cshtml
@{
Layout = null;
}
@model IEnumerable<WebApplication.Models.TopMenu>
<ul id="nav" class="sf-menu" >
<li><a href="#">MainPage</a></li>
@{
foreach (var item in Model)
{
<li>
@if (item.ParentId == null && Model.Where(a => a.ParentId == item.Id).Any())
{
<a href="/PageDetail/@[email protected]">@item.Name</a>
}
else if (item.ParentId == null && Model.Where(a => a.ParentId != item.Id).Any())
{
var link = "";
if (!String.IsNullOrEmpty(item.Link)) { link = item.Link; }
else { link = "/PageDetail" item.Name "-" item.Id; }
<a href="@link">@item.Name</a>
}
<ul>
@foreach (var item1 in Model.Where(a => a.ParentId == item.Id))
{
<li><a style="text-transform:none;" href="/PageDetail/@[email protected]" >@item.Name</a></li>
}
</ul>
</li>
}
}
</ul>
我正在嘗試使用 database 顯示頂部選單名稱。不起作用并且沒有給出錯誤。我嘗試使用視圖包進行遷移,并且我正在使用 ms sql。我應該怎么做才能獲得選單名稱?
另一種方法
MainPageController.cs
public IActionResult MainPage()
{
// ViewBag.cars = db.Cars.OrderByDescending(a => a.date).Take(6);//view bag works for cars
var cars = db.Cars.OrderByDescending(a => a.date).Take(11);//it works this way too
//ViewBag.menus = db.TopMenu.Where(a => a.Active).OrderBy(a => a.Order).ToList();
return View(cars);
}
public IActionResult Menu() {
var menus = db.TopMenu.OrderByDescending(a => a.Active).OrderBy(a => a.Order);
return View(menus );
}
注意:汽車資料作業正常
_Layout.cshtml
<div class="col-md-8">
<!-- Menü Partial -->
@await Html.PartialAsync("~/Views/MainPage/TopMenu.cshtml");
</div>
TopMenu.cshtml 錯誤

uj5u.com熱心網友回復:
1_create _ViewImports.cshtml 在 Views 檔案夾中寫下這一行
@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers
2_在Layout.cshtml中使用tagHelper
@{
var menus = (IEnumerable<WebApplication.Models.TopMenu>)ViewBag.menus;
}
<div class="col-md-8">
<!-- Menu Partial -->
<partial name="TopMenu.cshtml" model="@menus" />
</div>
其他
<partial name="~/Views/MainPage/TopMenu.cshtml" model="@menus" />
uj5u.com熱心網友回復:
這可能是因為您忘記在 html 標記中添加資料。
<a style="text-transform:none;" href="/PageDetail/@[email protected]" >@item.Title</a>
uj5u.com熱心網友回復:
我和你有同樣的問題,在布局中渲染區域視圖,如果區域視圖有模型,在這種情況下,我們可以使用ViewBag傳遞模型。但是如果我們在每個頁面中使用Layout頁面,我們需要ViewBag在相應的action中重寫。
選項1:
在控制器中:
public IActionResult Index()
{
var topMenu = new List<TopMenu>()
{
new TopMenu { Name = "Khushbu",Id= 1,Link = "Link1", ParentId = 1 },
new TopMenu { Name = "Mohan", Id = 2 ,Link = "Link2", ParentId = 2},
new TopMenu { Name = "John", Id = 3 ,Link = "Link3", ParentId = 3},
new TopMenu { Name = "Martin", Id= 4,Link = "Link4", ParentId = 4},
new TopMenu { Name = "Due", Id= 5,Link = "Link5", ParentId = 5 }
}; // you also can get menus from database
ViewBag.menus = topMenu;
return View();
}
public IActionResult Privacy()
{
var topMenu = new List<TopMenu>()
{
new TopMenu { Name = "Khushbu",Id= 1,Link = "Link1", ParentId = 1 },
new TopMenu { Name = "Mohan", Id = 2 ,Link = "Link2", ParentId = 2},
new TopMenu { Name = "John", Id = 3 ,Link = "Link3", ParentId = 3},
new TopMenu { Name = "Martin", Id= 4,Link = "Link4", ParentId = 4},
new TopMenu { Name = "Due", Id= 5,Link = "Link5", ParentId = 5 }
};
ViewBag.menus = topMenu;
return View();
}
我的 TopMenu.cshtml 在共享檔案夾中,在布局中添加:
@{
var menus = (IEnumerable<TopMenu>)ViewBag.menus;
}
<div class="col-md-8">
<!-- Menü Partial -->
@await Html.PartialAsync("~/Views/Shared/TopMenu.cshtml",menus);
結果:

所以,在這種情況下,我更喜歡使用ViewComponent. 閱讀
更新
[ViewComponent(Name = "TopMenu")]
public class TopMenuViewComponent : ViewComponent
{
private readonly ViewComponentMVCContext _context;
public TopMenuViewComponent(ViewComponentMVCContext context)
{
_context = context;
}
public async Task<IViewComponentResult> InvokeAsync()
{
var menus = await _context.TopMenu.ToListAsync();
return View("TopMenu", menus);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/475674.html
