问题
Razor pages have a mechanism where you can reference named sections in your layout, and then specify them in your pages that use that layout. For example, if your Layout (_Layout.cshtml) looks like this:
@using...
...
<!DOCTYPE html>
...
<body>
...
@RenderSection("modals", required: false)
...
and then in your dashboard page, for example, you'd have:
<div>
...
</div>
...
...
@section modals
{
<div class="modal-container>...</div>
<div class="modal-container>...</div>
}
that would inject the contents of @section modals...
into the place in the layout where @RenderSection("modals")
is.
How can this be done in Blazor?
回答1:
Unfortunately, no such feature is currently supported in Blazor. Blazor team and the community have been discussing the necessity of this feature over a year now, but for no avail. Read here about Sections in Blazor.
However, if you're looking for a 'temporary solution', you may read a comment by SteveAndeson about how to pass parameter values from a Blazor component page to its layout. I know that you're looking for a way to render the @Body plus @EndBody, but the principal remain the same: You have to create a layout component instead of the default layout
Hope this helps...
回答2:
I have created a component to get something similar to sections:
https://github.com/inspgadget/BlazorSections
来源:https://stackoverflow.com/questions/57912935/rendersection-equivalent-in-blazor