在剃刀输出单引号生成的JavaScript字符串 [英] Output single quotes in Razor generated JavaScript String

查看:174
本文介绍了在剃刀输出单引号生成的JavaScript字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我组装使用剃刀在JavaScript中的几行。我想最简单的方法是首先组装整个的JavaScript块,然后输出整个事情。问题是,单引号被渲染为&放大器; #39;

是否有可能改变的最后一行得到这个正确写:

  VAR friendArray =新的Array();
    @ {
        INT I = 0;
        字符串jsString =;
        的foreach(在朋友的朋友VAR)
        {
            jsString = jsString +friendArray [;
            jsString = jsString + i.ToString();
            jsString = jsString +] =';
            jsString = jsString + friend.displayname;
            jsString = jsString +';;
            我++;
        }
        @jsString;
    }

以上生成此:

  friendArray [0] =&安培; #39;&霍利斯特放大器; #39 ;; friendArray [1] =&放大器; #39;&费斯图斯放大器; #39 ;;


解决方案

您可以通过输出这种方式关闭的HTML编码:

  @ Html.Raw(jsString)

I am assembling a few lines in JavaScript using Razor. I thought the easiest way would be to assemble the entire JavaScript block first, then output the entire thing. The problem is, the single quotes are being rendered as & #39;.

Is it possible to change the last line to get this to write correctly:

    var friendArray = new Array();
    @{
        int i = 0;
        string jsString="";
        foreach(var friend in friends)
        {
            jsString = jsString + "friendArray[";
            jsString = jsString + i.ToString();
            jsString = jsString + "]='";
            jsString = jsString + friend.displayname;
            jsString = jsString + "';";
            i++;
        }
        @jsString;
    }

The above generates this:

  friendArray[0]=& #39;Hollister& #39;;friendArray[1]=& #39;Festus& #39;;

解决方案

You could turn off the encoding of HTML by outputting this way:

@Html.Raw(jsString)

这篇关于在剃刀输出单引号生成的JavaScript字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆