I wanted to access a session variable in javascript in asp.net mvc application. I have found a way to do it in aspx view engine but not in razor.
Please tell me a way to access the session variables
I wanted to access a session variable in javascript in asp.net mvc application. I have found a way to do it in aspx view engine but not in razor.
Please tell me a way to access the session variables
You can do it this way for a
String
variable:Or like this if it's numeric:
This is really not a very clean approach though, and requires inline JavaScript rather than using script files. Be careful not to get carried away with this.
I personally like the data attribute pattern.
In your Razor code:
In your javascript:
In my asp.net I am not getting the result by
But I get the answer by below code,
For google searchers,
In addition, If you want to access the session variable in external
.js
file you can simply do like this,Inside
NewFile.js