I use razor page
to develop a website and I'm a newbie. I want load data for Menu partial page but I don't know how. My structure as below
Can you help me how to do it? Or give me a refer link? Thanks
CodePudding user response:
The optimal solution for this task is a view component (https://www.learnrazorpages.com/razor-pages/view-components). You create a class that inherits from ViewComponent
e.g. MenuViewComponent, and inject your context or data service into the constructor, and use it in an InvokeAsync
method to generate a model that it passed to the view. The view is a simple razor file named default.cshtml located at /Pages/Shared/Components/<component name>/
, where <component name>
in this example is Menu.