4
@{
    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?

1 Answers1

6

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>
Kenneth
  • 28,294
  • 6
  • 61
  • 84