Reputation: 714
I just noticed when I do something like this:
@{
String var1 = "there's error to this string";
}
<script>
alert('@var1');
</script>
when I start running this and I look at alert box, the single quote character is transforming into weird display starting with #.
but when I just directly placing the string value to alert box, it works as expected:
<script>
alert("there's error to this string");
</script>
I hope somebody could explain here why it happens.
Upvotes: 0
Views: 394
Reputation: 887767
You need to Javascript-encode the string, then tell Razor not to HTML-encode it:
@Html.Raw(HttpUtility.JavaScriptStringEncode(var1))
Upvotes: 2