kez kez - 14 days ago 5
C# Question

define a long array in jquery click function

I have following long type session on server side code

long[] grouparray = ..;

Session["grouplist"] = grouparray;


Now I'm trying to get this session on View Page's jquery click function

$("#gpline").click(function () {

parseInt(@Session["grouplist"]);
var grouplistvalues = Session["grouplist"];

alert(grouplistvalues);

});


But this is having error once I debug using firebug


SyntaxError: expected expression, got ']'

parseInt(System.Int64[]);

Answer

You need to use Json.Encode and @Html.Raw on your c# data to make it compatible with your scripts.

Try this.

$("#gpline").click(function () {

    var grouplistvalues = @Html.Raw(Json.Encode(Session["grouplist"])); // converting the session data into array of numbers in javascript variable

    alert(JSON.stringify(grouplistvalues)); // stringify is used only to test.

});
Comments