How do I render a Boolean to a JavaScript variable in a cshtml file?
Presently this shows a syntax error:
<script type="text/javascript" >
var myViewModel = {
isFollowing: @Model.IsFollowing // This is a C# bool
};
</script>
How do I render a Boolean to a JavaScript variable in a cshtml file?
Presently this shows a syntax error:
<script type="text/javascript" >
var myViewModel = {
isFollowing: @Model.IsFollowing // This is a C# bool
};
</script>
Because a search brought me here: in ASP.NET Core,
IJsonHelper
doesn't have anEncode()
method. Instead, useSerialize()
. E.g.:Here's another option to consider, using the !! conversion to boolean.
This will generate the following on the client side, with 1 being converted to true and 0 to false.
The JSON boolean must be lowercase.
Therefore, try this (and make sure nto to have the
//
comment on the line):Or (note: you need to use the namespace
System.Xml
):Why
True
and nottrue
you ask... Good question:Why does Boolean.ToString output "True" and not "true"
You may also want to try:
and an ever better way is to use: