简体   繁体   中英

Asp.net core 2.1 - Layout controller

I'm learning asp.net core 2.1. I made a database with Menu table ( ID, Name, Actio,Controller) And i wanted to pass data from database to menu. To do that i need a controller who will pass data to view. How to pass these data to _Layout.cshtml? I will need it for child (partial view "Menu").

In templete mvc there is no controller for Layout...

In the _layout.cshtml you could simply have a

@Html.Action("Method", "Controller")

in the body which you can use to call another controller. Ideally if this is a navbar etc I'd return a partialView from this method.

I managed to do it by using ViewComponent

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.1

@Html.Action("Method", "Controller") Doesn't work in Asp.Net CORE 2.1

In Core 2.1 You need to use

<partial name="PartialViewName"/> instead of @Html.Action("PartialViewName","ControllerName")

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/partial-tag-helper?view=aspnetcore-2.2 See here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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