I want to access a Session value in jquery method in the ASP.NET MVC view page. See the below code,
$('input[type=text],select,input[type=checkbox],input[type=radio]').attr('disabled', '<%= Session["CoBrowse"].ToString() %>');
How can I get the Session value in jquery.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
$('input,select').attr('disabled','<%=Session["CoBrowse"].ToString() %>');
Method 2
Many comments:
- You cannot “Access Session from jQuery”. You use MVC and asp.net to create an HTML page (with JavaScript). Session is a server-side object, and JavaScript runs on the client side.
- Take a look at jQuery’s selectors. They have useful selectors like
:checkbox,:text, etc. - Your code produce the JavaScript you expect: it compiles, runs, and produces JavaScript with
trueorfalseon the right place. - This is not the way to disable an element. If an element has the ‘disabled’ attribute it will be disabled, no matter the value.
<input type="checkbox" disabled="false" />is also a disabled check box, so your controls are always disabled. -
If that is the way you choose anyway, consider:
var isCoBrowse = <%= Session["Name"].ToString().ToLower() %>; if(!isCoBrowse) //disable controls $(":text,:checkbox,:radio").attr("disabled","disabled"); //standard.This will produce the client-side JavaScript code:
var isCoBrowse = true;
And, to enable an element:
$("input").removeAttr("disabled");
Also, there are much better ways to accomplish this. Have you considered disabling the controls on server side, if possible?
Method 3
Not sure if this is the best route but within your aspx page you could create a method that returns the value of your session variable, e.g.
Server side:
using System.Web.Services;
[WebMethod(EnableSession = true)]
public static string GetSession()
{
return Session["CoBrowse"].ToString();
}
then call this method client side using jQuery:
$.ajax({
type: "POST",
url: "./Default.aspx/GetSession",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result){
('input[type=text],select,input[type=checkbox],input[type=radio]').attr('disabled', result.d);
}
});
Method 4
<input id="sessionInput" type="hidden" value='<%= Session["name"] %>' />
var getSessionValue = $('#sessionInput').val();
Method 5
If that Session variable is sensitive data (which in this case it probably isn’t), I would not use this solution as it would show the Session data when you looked at the javascript source. If it is sensitive session data, accessing it via a web method (see above answer) is probably best.
Method 6
Easy! when you know how:
@Html.Encode(Session(“classificationTitle”))
…and in the .js file:
var classificationTitle = document.getElementById('classificationTitle').innerHTML;
sorry – I can’t post the full html as this site strips out angle brackets 🙁
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0