最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Error when accesing Razor Boolean variable in Javascript - Stack Overflow

programmeradmin1浏览0评论

I have tried several tactics to use the boolean value within the JS ,but nothing works :

<script type="text/javascript">
var model = @Html.Raw(Json.Encode(Model));
       if (model.IsNew == true) {
        alert("1");
     }
</script>

Tried the following:

var IsNew = @Model.IsNew ;
var IsNew = "@Model.IsNew";

I keep getting the following error :

Conditional pilation is turned off

Anyone could explain why this occurs and maybe guide me to a possible solution ?

I have tried several tactics to use the boolean value within the JS ,but nothing works :

<script type="text/javascript">
var model = @Html.Raw(Json.Encode(Model));
       if (model.IsNew == true) {
        alert("1");
     }
</script>

Tried the following:

var IsNew = @Model.IsNew ;
var IsNew = "@Model.IsNew";

I keep getting the following error :

Conditional pilation is turned off

Anyone could explain why this occurs and maybe guide me to a possible solution ?

Share Improve this question edited Aug 28, 2012 at 16:18 tereško 58.5k25 gold badges100 silver badges150 bronze badges asked Aug 28, 2012 at 15:46 Mihai LaboMihai Labo 1,0822 gold badges17 silver badges40 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Try

if ('@Model.IsNew' == 'true') {
    alert("Is New");
 }

That's just the VS IDE failing to understand the mix of Razor and Javascript.

Your code will work fine.

发布评论

评论列表(0)

  1. 暂无评论