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

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745383712a4625346.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信