0

In my ASP.NET Core Code first project developed using ASP.NET Cor Web Application (.NET Core) template how can I add, for example, following jQuery code on a specific view, say, test.cshtml so that it is included only on a page using this view. I'm using VS2015-Update3.1. Only those familiar with the folder structure of such a project in VS2015 can probably answer.

NOTE: I'm trying to add following script inline and not as a file.

$('.list-group-item').click(function(e){ 
  e.stopPropagation();
})
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
nam
  • 21,967
  • 37
  • 158
  • 332

1 Answers1

2

The standard _Layout.cshtml file that comes with the standard project template calls RenderSection() near the very bottom:

@RenderSection("scripts", required: false)

This tells the ViewEngine to insert a section called scripts (if present) from the rendered View (which gets inserted exactly in the place where RenderBody() is called.

// Test.cshtml

...
<ul class="list-group">
  <li class="list-group-item">Item-1</li>
  <li class="list-group-item">Item-2</li>
  <li class="list-group-item">Item-3</li>
</ul>
...

@section scripts
{
  <script>
    $('.list-group-item').click(function(e){ 
      e.stopPropagation();
    })
  </script>
}

So just define the section in your view and it will get inserted in the layout in the designated place.

ypsilo0n
  • 576
  • 5
  • 7
  • Thank you for explaining your solution. However, the jQuery code suggested by Taras Kumpanenko and Juan Ferreras in my another question posted [here](http://stackoverflow.com/q/38857469/1232087) is not working. Maybe it has something to do with their code or me using it wrong. Someone may help. Since you answered my this post, I'll mark it as an answer. – nam Aug 09 '16 at 21:19