Mihai Labo
Mihai Labo

Reputation: 1082

Error when accesing Razor Boolean variable in Javascript

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 compilation is turned off

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

Upvotes: 3

Views: 4119

Answers (2)

Cornel Urian
Cornel Urian

Reputation: 396

Try

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

Upvotes: 3

SLaks
SLaks

Reputation: 887413

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

Your code will work fine.

Upvotes: 2

Related Questions