3

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 ?

2 Answers 2

3

Try

if ('@Model.IsNew' == 'true') {
    alert("Is New");
 }
Sign up to request clarification or add additional context in comments.

1 Comment

you would need to do: == 'True'
2

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

Your code will work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.