2

I'm trying to use razor c# in my javascript. However it's telling me: The name 'isReport' does not exist in the current context

My code is this:

<script type="text/javascript">
    var isReport = false;
    @if(Model.columns != null)
    {
        isReport = true;
    }
    alert(isReport);
    if(isReport)
        $("#reports").dataTable();
</script>

I'm trying to do the same this as in this post I think... Mix Razor and Javascript code

Thanks!

Community
  • 1
  • 1
kralco626
  • 8,456
  • 38
  • 112
  • 169
  • Yes; and the answer to that question is also the answer to this one. – SLaks Apr 24 '13 at 15:50
  • But you aren't following the advice of the answers in that post. Use `` or `@:` to identify the `isReport = true` line as JavaScript and not Razor code. – Jason Berkan Apr 24 '13 at 15:52

1 Answers1

11

You can use @: on your isReport assignment (inside the if):

<script type="text/javascript">
    var isReport = false;
    @if(Model.columns != null)
    {
        @:isReport = true; //change is here
    }
    alert(isReport);
    if(isReport)
        $("#reports").dataTable();
</script>
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148