Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Razor C# with javascript [duplicate]

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!

like image 338
kralco626 Avatar asked Apr 24 '26 00:04

kralco626


1 Answers

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>
like image 71
mattytommo Avatar answered Apr 26 '26 14:04

mattytommo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!