Using Razor, how do I render a Boolean to a JavaScript variable?
The JSON boolean must be lowercase.
Therefore, try this (and make sure nto to have the //
comment on the line):
var myViewModel = {
isFollowing: @Model.IsFollowing.ToString().ToLower()
};
Or (note: you need to use the namespace System.Xml
):
var myViewModel = {
isFollowing: @XmlConvert.ToString(Model.IsFollowing)
};
You may also want to try:
isFollowing: '@(Model.IsFollowing)' === '@true'
and an ever better way is to use:
isFollowing: @Json.Encode(Model.IsFollowing)
Because a search brought me here: in ASP.NET Core, IJsonHelper
doesn't have an Encode()
method. Instead, use Serialize()
. E.g.:
isFollowing: @Json.Serialize(Model.IsFollowing)