使用Razor,如何将布尔值呈现给JavaScript变量?

时间:2022-12-01 16:40:19

How do I render a Boolean to a JavaScript variable in a cshtml file?

如何在cshtml文件中向JavaScript变量呈现布尔值?

Presently this shows a syntax error:

目前这显示了一个语法错误:

<script type="text/javascript" >

    var myViewModel = {
        isFollowing: @Model.IsFollowing  // This is a C# bool
    };
</script>

5 个解决方案

#1


204  

You may also want to try:

你也可以试试:

isFollowing: '@(Model.IsFollowing)' === '@true'

and an ever better way is to use:

更好的方法是:

isFollowing: @Json.Encode(Model.IsFollowing)

#2


19  

The JSON boolean must be lowercase.

JSON布尔值必须为小写。

Therefore, try this (and make sure nto to have the // comment on the line):

因此,试试这个(并确保nto在行上有//注释):

var myViewModel = {
    isFollowing: @Model.IsFollowing.ToString().ToLower()
};

Or (note: you need to use the namespace System.Xml):

或(注意:需要使用名称空间System.Xml):

var myViewModel = {
    isFollowing: @XmlConvert.ToString(Model.IsFollowing)
};

#3


10  

var myViewModel = {
    isFollowing: '@(Model.IsFollowing)' == "True";
};

Why True and not true you ask... Good question:
Why does Boolean.ToString output "True" and not "true"

你会问,为什么是真的,为什么不是真的……好问题:为什么布尔值?ToString输出“True”而不是“True”

#4


10  

Because a search brought me here: in ASP.NET Core, IJsonHelper doesn't have an Encode() method. Instead, use Serialize(). E.g.:

因为一个搜索把我带到了这里:在ASP。NET Core, IJsonHelper没有Encode()方法。相反,使用序列化()。例如:

isFollowing: @Json.Serialize(Model.IsFollowing)    

#5


3  

Here's another option to consider, using the !! conversion to boolean.

这里还有一个可以考虑的选项,使用!!转换为布尔值。

isFollowing: !!(@Model.IsFollowing ? 1 : 0)

This will generate the following on the client side, with 1 being converted to true and 0 to false.

这将在客户端生成以下内容,其中1被转换为true, 0被转换为false。

isFollowing: !!(1)  -- or !!(0)

#1


204  

You may also want to try:

你也可以试试:

isFollowing: '@(Model.IsFollowing)' === '@true'

and an ever better way is to use:

更好的方法是:

isFollowing: @Json.Encode(Model.IsFollowing)

#2


19  

The JSON boolean must be lowercase.

JSON布尔值必须为小写。

Therefore, try this (and make sure nto to have the // comment on the line):

因此,试试这个(并确保nto在行上有//注释):

var myViewModel = {
    isFollowing: @Model.IsFollowing.ToString().ToLower()
};

Or (note: you need to use the namespace System.Xml):

或(注意:需要使用名称空间System.Xml):

var myViewModel = {
    isFollowing: @XmlConvert.ToString(Model.IsFollowing)
};

#3


10  

var myViewModel = {
    isFollowing: '@(Model.IsFollowing)' == "True";
};

Why True and not true you ask... Good question:
Why does Boolean.ToString output "True" and not "true"

你会问,为什么是真的,为什么不是真的……好问题:为什么布尔值?ToString输出“True”而不是“True”

#4


10  

Because a search brought me here: in ASP.NET Core, IJsonHelper doesn't have an Encode() method. Instead, use Serialize(). E.g.:

因为一个搜索把我带到了这里:在ASP。NET Core, IJsonHelper没有Encode()方法。相反,使用序列化()。例如:

isFollowing: @Json.Serialize(Model.IsFollowing)    

#5


3  

Here's another option to consider, using the !! conversion to boolean.

这里还有一个可以考虑的选项,使用!!转换为布尔值。

isFollowing: !!(@Model.IsFollowing ? 1 : 0)

This will generate the following on the client side, with 1 being converted to true and 0 to false.

这将在客户端生成以下内容,其中1被转换为true, 0被转换为false。

isFollowing: !!(1)  -- or !!(0)