Search code examples
c#html.netasp.net-mvcrazorengine

Why does RAZOR output a capitalized boolean value?


@{
    bool a = false;
 }    

<script>

var element = '<a href="www.testurl.com" + '?a=' + @(a)';
$('#anyElement').append(element);

</script>

The anchor tag will have the href of, www.testurl.com?a=False

In IE11, at least, variable a will be rendered as False instead of false. This causes an ECMAScript error because now the JavaScript/ECMAScript compiler thinks False is an undefined variable.

Why does RAZOR not render this variable in lowercase?


Solution

  • Razor just uses the ToString()-method of whatever you put there. In case of a bool, that will evaluate to True or False. To circumvent the problem, just lowercase it yourself:

    @{
        bool a = false;
    }    
    
    <script>
    
    var element = '<a href="www.testurl.com" + '?a.ToString().ToLower()=' +   @(a)';
    $('#anyElement').append(element);
    
    </script>