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!

2
  • Yes; and the answer to that question is also the answer to this one. Commented Apr 24, 2013 at 15:50
  • But you aren't following the advice of the answers in that post. Use <text> or @: to identify the isReport = true line as JavaScript and not Razor code. Commented Apr 24, 2013 at 15:52

1 Answer 1

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>
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.