18

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?

7 Answers 7

18

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.

EDIT: Note the difference between:

<script type="text/javascript">
var myBoolean = <%= (myBoolean ? "true" : "false") %>;
</script>

and

<script type="text/javascript">
var myBoolean = '<%= (myBoolean ? "true" : "false") %>';
</script>

In the first example, you end up with:

var myBoolean = false;

and that's a literal Boolean false. In the second, you end up with:

var myBoolean = 'false';

and in JavaScript, 'false' is a non-empty string and consequently if evaluated in a Boolean context, it'll be true. Well, true-ish. :)

Sign up to request clarification or add additional context in comments.

7 Comments

actually 1 is not true in javascript, but 0 is false.
Am I right or is 1 representing true also, I guess w3schools could be wrong as it doesn't make sense hehe.
1 is trueisch, eg. if(1){alert("will happen");} but if(1===true){alert("will never happen");}
Apparently the string "false" evaluates to true in Javascript so this would only work when your value is true. if myBoolean is false then the javascript variable would be val theValue = "false" which would equal true not false.
@StephenPrice Truthiness in Javascript is quite bizarre. Every date/time in Javascript is true except for the stroke of midnight on Dec 31st, 1969 (which evaluates to zero, because JS uses 01/01/1970 00:00:00 as the epoch) I once mentioned this in a JS talk, and this old hippie coder piped up with "yeah, I was there... midnight on New Year's Eve. Armstrong had walked on the moon, we'd just had the summer of love, everything looked absolutely wonderful and optimistic. Looking back on it, it was so obviously false..." :)
|
6

I created an extension method to use it in any boolean property of a Model.

    public static class GeneralExtensions
    {
    public static string GetValueForJS(this bool argValue)
        {
            return argValue ? "true" : "false"; 
        }
     }

Now in a view I can simply use:

<script type="text/javascript">
    var variable = @Model.IsFoo.GetValueForJS();
</script>

1 Comment

+1 I would call it something shorter like "ToJS", but It's better than polluting the views with '? "true" : "false"'
3

if you need to do this often, just add this to the top of the javascript (or your js library file, etc.)

var True = true; False = false;

in a simple one off case use:

var x = ('<%= boolValue %>'=='True' );

Comments

2

For me, this approach works (MVC4)

<script type="text/javascript">

if (@{if (Model.IsSubmitted) { @Html.Raw("true"); } else { @Html.Raw("false"); } } == true)
{
// code
}

</script>

1 Comment

Or simply if (@Html.Raw(Model.IsSubmitted?"true":"false"))
1

You can use the javascript Boolean() method

if (Boolean(value)) {}

2 Comments

Boolean("false") is true not false.
This doesn't work: Boolean(@Model.MyBool) will give you Boolean(True) or Boolean(False), both of which will give you an error that True or False is undefined.
0

You want to use this:

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

2 Comments

Please tell me this namespace overload is a joke.
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. msdn.microsoft.com/en-us/library/…
0

I wrote a public js method, when i need to check a passed boolean variable from action in javaScript, just send the original value to this method to parse.

function GetBooleanValue(value) {
if (value == undefined) return false;

var parsedValue = value.toLowerCase();

if (parsedValue === "false") return false;
else if (parsedValue === "true") return true;
else return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.