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.
您需要对字符串进行Javascript编码,然后告诉Razor不要对其进行HTML编码:
@Html.Raw(HttpUtility.JavaScriptStringEncode(var1))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.