0
votes

Using Razor Pages @RenderSection does not seem to be available on a Razor Page, I get "RenderSection does not exist in the current context"

I have a number of Razor pages where I am trying to use a partial page on the partial page the is some accompanying css and Javascript I would like injected into the css and script section

If I just define the sections on the partial page nothing gets rendered, so after some searching it seems that on the page using the partial you need to add RenderSection in the various sections.

Any help would be appreciated.

1

1 Answers

1
votes

May be you have missed the point on usage of @RenderSection in ASP.NET Core. @RenderSection should be only in Layout page as follows:

 <script src="~/lib/jquery/dist/jquery.js"></script>
 @RenderSection("Scripts", required: false)

Then Razor page should be as follows:

@{
    ViewData["Title"] = "My Razor Page";

    Layout = "_Layout"; // If this specified in the `_ViewStart.cshtml` then you don't need it
}

<partial name="_YourPartial.cshtml"/>
@section scripts {
    <script src="~/js/yourjs.js" asp-append-version="true"></script> // this is specific to this Razor page only and it will also be available on the partial view called inside this Razor Page
}

And during the generation of html, the scripts on your Razor Page will be rendered as follows:

<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/js/yourjs.js" asp-append-version="true"></script>

Hope it will make you clear!