Reputation: 39108
I've seen a cool gadget and I want to use it on my page. I've chosen all the options and they produced a JS that I'm supposed to execute from my site.
Since I'm using Razor, I need to render it as a section so I've added one. However, as I attempted to render it, I got error message telling me that:
The file "~/Views/Home/EyeCandy.cshtml" cannot be requested directly because it calls the "RenderSection" method.
So I've googled and found a bunch of articles explaining how to resolve it. The best one (after a few hours) is this particular reply. However, I just can't wire my head around it and get confused.
@{ Layout = "~/Views/Shared/_Default.cshtml"; }
@model List<Donkey>
<h1>List of the eye candies.</h1>
@RenderSection("Footie", false)
@section Footie { <script src="@Url.Content("...")"
async="async"
type="text/javascript"></script> }
In the linked reply, there are three files.
Now, in my head, it doesn't make sense, because the point is to keep the script out of the layout and limit its scope to the one view where I test my eye candies. I'm sure I'm just too slow to get it at this stage but if someone, pretty-pretty please, points out what I missunderstood, I'll be so happy.
Upvotes: 0
Views: 4231
Reputation: 218732
You need to call the @RenderSection("Footie", false)
in your layout, not in your specific view.
So add this to your ~/Views/Shared/_Default.cshtml
@RenderSection("Footie", false)
And in your specific view, you can execute your custom code, which is wrapped inside the Footie section
@section Footie {
<script src="@Url.Content("...")" async="async" type="text/javascript"></script>
}
Upvotes: 3