Link to home
Start Free TrialLog in
Avatar of websss
websssFlag for Kenya

asked on

Float array in c# - when passed to JS it's in wrong order

Hi, I have the float array which is populated in c#

temperatureData= new float[570, 429];

Open in new window



I then prepare the data in c# to be send to the front end by doing the following (this might be where its going wrong)

System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();
jSearializer.MaxJsonLength = int.MaxValue;
pd.TemperatureDataArray = jSearializer.Serialize(temperatureData);

ViewBag.TempDataArray = pd.TemperatureDataArray;

Open in new window



then in the front end i have

var tempDataArray = @(ViewBag.TempDataArray);


var imageTemperatureDataArray = JSON.stringify(tempDataArray);
imageTemperatureDataArray = JSON.parse(imageTemperatureDataArray);


alert(imageTemperatureDataArray[321, 228]);

Open in new window


However the value in position 321,228 in JS is not the same as in C#, so i assume its re-ordered the data some how


Can anyone advise where its getting muddled up?


I've attached the dataset which is was taken on a BP on this line
ViewBag.TempDataArray = pd.TemperatureDataArray


The correct value in the float array at this position  [321, 228] should be 346.795624
However in the front end i'm seeing 304.92453
Avatar of ste5an
ste5an
Flag of Germany image

Have you check your indices? Not that you're off by one..
Avatar of websss

ASKER

Yes, I did check the surrounding values, it's not the issue
SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of websss

ASKER

Thanks

I implemented what you said

On this line i get UNDEFINED
alert(imageTemperatureDataArray[321][228]);

Open in new window


the console.write output is in the attached file
consolewrite.txt
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of websss

ASKER

thanks all
Kelvin, this was the bit that got it working
@Html.Raw(ViewBag.TempDataArray);
Avatar of websss

ASKER

Hey guys, sorry for bringing this up again, but I have a slightly different issue

I've also accessed this float array from a ajax url.action Post
In the CS i return an object in JSONResult
However its a javascript variable at this point and cannot run @Html.Raw(ViewBag.TempDataArray);
its coming back as
[[273.7,273.8,273.8,274.6,274.4 etc

And I need to convert it to array as this did: @Html.Raw(ViewBag.TempDataArray);

Any ideas?
I commented there, requesting your Controller method code.