'; var session_value = '%=Session["Test"]%>'; alert(session_value);its giving "%=Session[" name="description" /> '; var session_value = '%=Session["Test"]%>'; alert(session_value);its giving "%=Session[" />
36995

How to create session variable and retrieve in JavaScript MVC3

Question:

I want to create a variable Session in JavaScript and Retrieve it in same JavaScript

My Code

'<%Session["Test"] = "Welcome DS";%>'; var session_value = '<%=Session["Test"]%>'; alert(session_value);

its giving "<%=Session["Test"]%>" alert result, Its not giving Session values

Answer1:

You are using a wrong syntax that is for another version of razor. You can change it to:

alert('@Session["Test"]');

Notice the use of the @ sign. You can <a href="http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3razor.aspx" rel="nofollow">refer to this page</a> for the proper syntax.

Answer2:

ASP.NET thinks what you write is a plain string. Removing the quotation marks should do the trick if we're talking about an .aspx file.

<%Session["Test"] = "Welcome DS";%> var session_value = <%=Session["Test"]%>;

Answer3:

If you really need your session variable only in Javascript why donøt you use a client side Session variable?

for example the html5 <a href="https://code.google.com/p/sessionstorage/" rel="nofollow">sessionStorage</a>

You can set it like this:

sessionStorage.myVariable = "myvalue";

and read it like this:

var x = sessionStorage.myVariable

Recommend

  • Play Framework nested form errors missing
  • Download/Save/Write a file on the client's hard disk using flash/flex
  • Base Internationalization and “Could not find a storyboard named […]”
  • UITableView takes much longer to load when numberOfRows returns a large number
  • CSS: How to fix overlapping divs
  • R sqldf renaming a field in a select statement
  • Available space left on drive - WinAPI - Windows CE
  • ZipList with Scalaz
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • SonarQube: Cannot deactivate rule with missing quality profile
  • Question about instantiating object
  • SAXReader not re-ecape characters
  • How to add a focus style to an editable ComboBox in WPF
  • Diff between two dataframes in pandas
  • How can I set a binding to a Combox in a UserControl?
  • Ajax calls do not work in IE unless you fiddle with security settings
  • How to use JavaScript to determine whether a file exists in a directory?
  • Functions in global context
  • Email verification using google app script and google forms
  • Refering to the class itself from within a class mehod in Objective C
  • Meteor: Do Something On Email Verification Confirmation
  • FileReader+canvas image loading problem
  • Deserializing XML into class C#
  • Where to put my custom functions in Wordpress?
  • Javascript + PHP Encryption with pidCrypt
  • Convert array of 8 bytes to signed long in C++
  • Apache 2.4 - remove | delete | uninstall
  • RestKit - RKRequestDelegate does not exist
  • php design question - will a Helper help here?
  • Compare two NSDates in iPhone
  • How to include full .NET prerequisite for Wix Burn installer
  • AngularJs get employee from factory
  • WPF Applying a trigger on binding failure
  • Understanding cpu registers
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • costura.fody for a dll that references another dll
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize