I have a View Component that contains some jQuery in the Razor (.cshtml) file. The script itself is quite specific to the view (deals with some some configuration of third-party libraries), so I would like to keep the script and HTML in the same file for organization's sake.
The problem is that the script is not rendered in the _Layout Scripts section. Apparently this is just how MVC handles scripts with regards to View Components.
I can get around it by just having the scripts in the Razor file, but not inside of the Scripts section.
But then I run into dependency issues - because jQuery is used before the reference to the library (the reference to the library is near the bottom of the _Layout file).
Is there any clever solution to this other than including the reference to jQuery as part of the Razor code (which would impede HTML rendering where ever the component is placed)?
I'm currently not in front of the code, but I can certainly provide it once I get the chance if anyone needs to look at it to better understand it.
What I decided to do is write a ScriptTagHelper that provides an attribute of "OnContentLoaded". If true, then I wrap the inner contents of the script tag with a Javascript function to execute once the document is ready. This avoids the problem with the jQuery library having not loaded yet when the ViewComponent's script fires.
example usage within a ViewComponent:
There might be a better way than this, but this has worked for me so far.
Here's the solution I'm using. It supports both external script loading and custom inline script. While some setup code needs to go in your layout file (e.g. _Layout.cshtml), everything is orchestrated from within the View Component.
Layout file:
Add this near the top of your page (inside the
<head>
tag is a good place):Add this near the bottom of your page (just before the closing
</body>
tag is a good place):View Component:
Now anywhere in your code, including your View Component, you can do this and have the script executed at the bottom of your page:
With External Script Dependency
But sometimes that's not enough. Sometimes you need to load an external JavaScript file and only execute your custom script once the external file is loaded. You also want to orchestrate this all in your View Component for full encapsulation. To do this, I add a little more setup code in my layout page (or external JavaScript file loaded by the layout page) to lazy load the external script file. That looks like this:
Layout file:
Lazy load script
View Component:
Now you can do this in your View Component (or anywhere else for that matter):
To clean things up a bit, here's a tag helper for the View Component (inspired by @JakeShakesworth's answer):
Now in my View Component I can do this:
You can also leave out the
after
attribute if the View Component doesn't have an external file dependency.You can use it to
render sections
for aLayout
that thepartial view
orview component's
view declares. However, sections defined in partials and view components don't flow back to the rendering view or it's Layout. If you want to use jQuery or other library reference in your partial view or View component then you can pull your library intohead
rather thanbody
in yourLayout
page.Example:
View Component:
Location of ViewComponent:
Default.cshtml:
_Layout.cshtml :
View.cshtml:
View Component by grahamehorner: (you can more information here)