0

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.

  1. _PartialScripts.cshtml, which I don't have and will need to add populating it from the script tag in my section Footie.
  2. _Partial.cshtml, which is my current view called EyeCandy.cshtml.
  3. The unnamed code snippet, which I understand corresponds to my _Default.cshtml layout.

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.

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

1 Answers1

3

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> 
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • 1
    Okay. So the rendering went just above the end of body and the section declaration went to the view, at the bottom. When I runt the script saying *alert("Shyju awesome");* it displays, so I guess the question's answered. Now, when I attempt doing the same with the globe-script, I get nothing on the screen but pasted URL in the FF actually gets me a function. Is it something you see obvious? If not, I'll another question, instead. This one's been definitely answered. – Konrad Viltersten Jan 02 '16 at 18:48
  • I posted an answer to your other question. – Shyju Jan 02 '16 at 21:05