jquery - Get a Session value from my MVC app into my Javascript -
this isn't standard, please read before marking duplicate.
in our mvc app @ top of our _layout.cshtml
, load our scripts this:
@scripts.renderformat("<script src='{0}' defer></script>", "~/bundles/scripts")
at bottom of _layout.cshtml
have this:
@rendersection("scripts", required: false)
for reasons outside of control must have defer on that. :(
this bundle includes jquery files. 1 of other files called script.js
, gets loaded after jquery, , has function called setcollapse(collapse)
.
it looks this:
function setcollapse(collapse) { debugger; alert(collapse); if (collapse == 'false') { $('.collapse').collapse("show"); } else { $('.collapse').collapse(); } }
i use session value javascript function on mvc view load this:
@section scripts { <script> $(document).ready(function() { debugger; var collapse = '@session["collapse"].tostring()'; setcollapse(collapse); }); </script> }
but continue get:
uncaught referenceerror: $ not defined
how can session value passed javascript/jquery when page loads?
here's how worked out. of course simpler figured.
in our mvc app @ top of our _layout.cshtml
this:
<script> var collapse = '@session["collapse"].tostring()'; </script> @scripts.renderformat("<script src='{0}' defer></script>", "~/bundles/scripts")
the renderformat
loads jquery , script.js
file.
then in script.js this:
$(document).ready(function () { //.... // global collapse value if (collapse == 'false') { $('.collapse').collapse("show"); } else { $('.collapse').collapse("hide"); } });
Comments
Post a Comment