This isn't the standard, so please read before marking duplicate.
In our MVC app at the top of our _Layout.cshtml, we load our scripts like this:
@Scripts.RenderFormat("<script src='{0}' defer></script>", "~/bundles/scripts")
At the bottom of _Layout.cshtml we have this:
@RenderSection("scripts", required: false)
For reasons outside of my control we must have the defer on that. :(
This bundle includes the jquery files. One of the other files is called Script.js, which gets loaded after jQuery, and has a function called setCollapse(collapse).
It looks like this:
function setCollapse(collapse) {
debugger;
alert(collapse);
if (collapse == 'False') {
$('.collapse').collapse("show");
} else {
$('.collapse').collapse();
}
}
I would like to use a Session value with that javascript function on my MVC View load like this:
@section scripts
{
<script>
$(document).ready(function() {
debugger;
var collapse = '@Session["Collapse"].ToString()';
setCollapse(collapse);
});
</script>
}
But I continue to get:
Uncaught ReferenceError: $ is not defined
How can I get my Session value to get passed into my javascript/jQuery when the page loads?