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.

2
  • 3
    Surround the console.log lines with <text> ... </text> tags. Commented Mar 23, 2016 at 20:30
  • Silly mistake, thank you @Cᴏʀʏ. Commented Mar 23, 2016 at 20:33

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.