Visual Basic .NET » ASP.NET General Discussion
Not converting JavaScript arrays?! -- JasonBunting --


JavaScript code:

var foo =
;
ASP.mypage_aspx.SaveData(foo);

C# code:


public int SaveData(object analysisElementValues)
{
return 0;
}

The problem: if I call GetType() on the analysisElementValues variable,
it indicates it is a String. Why?! I expected this to be converted to an array of arrays of integers (int
). Please either tell me how I can get that, or add this to a bug fix. It would seem that AjaxPro.net should know how to convert a native javascript array to a C# array,
since this is such a very basic data type.

-- MichaelSchwarz --


Hi,

the problem is the use of the type "object". If I get a

json string what data type should it be? int
...?

You can use:
public int SaveData(int
analysisElementValues)

Or...
public int SaveData(IJavaScriptObject analysisElementValues)
public int SaveData(JavaScriptArray analysisElementValues)

Regards,
Michael


On 7/12/06, JasonBunting <jason.bunting> wrote:

JavaScript code:

var foo =
;
ASP.mypage_aspx.SaveData(foo);

C# code:


public int SaveData(object analysisElementValues)
{
return 0;
}

The problem: if I call GetType() on the analysisElementValues variable,
it indicates it is a String. Why?! I expected this to be converted to
an array of arrays of integers (int
). Please either tell me how I
can get that, or add this to a bug fix. It would seem that AjaxPro.net
should know how to convert a native javascript array to a C# array,
since this is such a very basic data type.
>

--  

Best regards | Schöne Grüße Michael

Microsoft MVP - Most Valuable Professional Microsoft MCAD - Certified Application Developer

http://weblogs.asp.net/mschwarz 
/
http://www.schwarz-interactive.de 
/
mailto:info
-- JasonBunting --



Michael Schwarz wrote:

json string what data type should it be? int
...?

Well, your component should use the simplest one that makes sense, IMO. 

i.e. you attempt to coerce the JSON data to and int (call int.TryParse()) and if that doesn't work, try a long, etc. until it converts without a problem, and if all of those fail, then just pass it as a string array. I don't know, maybe I am not thinking it through all the way, but this seems to me to a no-brainer. Besides, can't you safely assume that if the server-side method accepts an int
, that you should be able to convert the JSON data to that data type? If it fails, the developer will just have to know what is going on. I mean, I know I am passing an array of integer arrays, so if I use the wrong data type on my server-side method, that is my problem. This seems like a no-brainer to me, but maybe I am not thinking it through all the way....

You can use:
public int SaveData(int
analysisElementValues)

That doesn't seem to be working for me. The server-side method never gets called and I don't get any error messages from your component. 


Or...
public int SaveData(IJavaScriptObject analysisElementValues)
public int SaveData(JavaScriptArray analysisElementValues)

Don't really want to use those, since they are your objects and again, 

I just don't see why a regular int
will not work....

-- JasonBunting --


So, are you just going to ignore my follow-up questions and comments? I really think you should address this issue - sending a simple JavaScript array back to the server should easily translate to a .NET array, and I don't see it being that hard to get it to work. It really is too bad you don't open source this project properly, maybe then those of us that have the time can help work on it. There are many that still don't understand why you insist on keeping the code to yourself and not letting others submit bug fixes, etc.

-- AlbertWeinert --


JasonBunting schrieb:

So, are you just going to ignore my follow-up questions and comments? I
really think you should address this issue - sending a simple
JavaScript array back to the server should easily translate to a .NET

It is no Problem for Ajax.NET Pro to convert JavaScripts array back to  

Server, if on the server side the type es defined. With XmlHttpRequest
only a string with chars is passed to the server, without some
information of the type it is not possible to converting something in
something special.
// client

var values=
;

PageMethods.DoMagic(values, theCallback);

function theCallback(res)
{
alert(res.value); //no error check in the example
}
// server

public class PageMethods
{
public int DoMagic(int
values)
{
int sum = 0;
foreach (int value in values)
{
sum+=value;
}
return 0;
}
}
// back to text

I think it makes no sense to make conversion on a good guess an TryParse
everything possible. With the right type defined of the server case,
there is not need for that.

In your case, you should use.

public int DoMagic(List<int
> values)
{
int sum = 0;
foreach (int
v in values)
{
foreach (int value in v)
{
sum += value;
}
}
return sum;
}

Works greats.

--
Freundliche Grüße

Albert Weinert

http://der-albert.com 

[Submit Comment]Home