2

This is what I'm trying to do in my view:

<script type="text/javascript">
    @if (ViewBag.scroll != null)
    {
        console.log("viewbag data: " + @ViewBag.scroll);
    }
    else
    {
        console.log("no viewbag data");
    }
</script>

I'd like to execute some jQuery code depending if the ViewBag has data, but it is giving me this error:

the name 'console' does not exist in the current context.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
Nick
  • 1,032
  • 16
  • 27

1 Answers1

1

You could assign it to a variable.

<script type="text/javascript">
    var hasScroll = @ViewBag.scroll != null;
    if (hasScroll) {
        console.log("viewbag data: " + @ViewBag.scroll);
    } else {
        console.log("no viewbag data");
    }
</script>
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56