views:

350

answers:

3

Hi,

I am passing a boolean value to a javascript function in my .net mvc action page.

problem is, it is outputting the value True and javascript apparently only accepts 'true' (in lower case).

I don't want to hack the variable and make it a string and convert it to lower case in my action, but it looks like I have no choice?

+2  A: 

If you're using the ToString() method on a .NET boolean to send the value to Javascript, try replacing it with something like

(myBoolean ? "true" : "false")

so that it gets sent to Javascript as the appropriate string representation of the required bool value.

Dylan Beattie
actually 1 is not true in javascript, but 0 is false.
mrblah
Am I right or is 1 representing true also, I guess w3schools could be wrong as it doesn't make sense hehe.
mrblah
1 is trueisch, eg. if(1){alert("will happen");} but if(1===true){alert("will never happen");}
svinto
A: 

You want to use this:

(.NET 3.5) System.Web.Script.Serialization.JavaScriptSerializer.Serialize

John Liu
Please tell me this namespace overload is a joke.
Rakesh Pai
in general you use the JSON serializer System.Runtime.Serialization.Json.DataContractJsonSerializer but JavaScriptSerializer is still around where you can use for simple direct type serializations.http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
John Liu
A: 

You can use the javascript Boolean() method
if (Boolean(value)) {}

bobhopeisgod