本文介绍了使用剃刀,我怎么渲染布尔到JavaScript变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何呈现一个布尔值,一个JavaScript变量在CSHTML文件?
presently这显示了一个语法错误:
<脚本类型=文/ JavaScript的> VAR myViewModel = {
isFollowing:@ Model.IsFollowing //这是一个T#布尔
};
< / SCRIPT>
解决方案
您也可以试试:
isFollowing:@(Model.IsFollowing)===@true
和一个以往更好的方法是使用
isFollowing:@ Json.En code(Model.IsFollowing)
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 T# bool
};
</script>
解决方案
You may also want to try:
isFollowing: '@(Model.IsFollowing)' === '@true'
and an ever better way is to use:
isFollowing: @Json.Encode(Model.IsFollowing)
这篇关于使用剃刀,我怎么渲染布尔到JavaScript变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!