Reputation: 31033
how can i access an array, defined in the form, inside javascript. e.g i have defined an integer array inside the web form like
<% using (Html.BeginForm())
{
int[] ctid = ViewData["ct"] as int[];
var x = 0;
%>
now i want to access this array(ctid) inside javascript, how can i do that...
Upvotes: 1
Views: 206
Reputation: 30498
You'll need to loop through the array and write it out to a Javascript array.
%>
//page JavaScript
var js_ctid = new Array(<% =ctid.length %>);
<%
//C# code
for (int=0; i<ctid.length; i++)
{
Response.Write("js_ctid[" + i + "] = " + js_ctid[i]);
}
You could also try the JavaScriptSerializer Class (http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx) to turn it into a JSON object
Upvotes: 2