c# - 脚本1015 : Unterminated string constant when loading string

标签 c# javascript string format

您好,我的 ASP.NET Razor v2 cshtml 文件中有此代码,该文件应将字符串加载到此段落中,该变量来自c# 字符串列表 但当加载的 string 包含如下内容时:

+ "<p>"+'Rejv&#237;zsk&#225; 113 79001 Jesen&#237;k'+"</p>"

我收到此 JavaScript 严重错误:SCRIPT1015:未终止的字符串常量

程序失败,因为某些字符转义了我的结束引号 '

实际上代码是这样的:

"<p>"+'@string.Format(serviceDescription[i])'+"</p>"

serviceDescription 是可能包含可以转义我的 ' 字符的任何值的字符串列表。

我可以知道如何纠正这个问题吗?

此代码用于调整Google API中的InfoWindow

到目前为止,我尝试过,但这些方法都不起作用:

  + "<p>"+' @string.Format("<p>{0}</p>", serviceDescription[i])'+"</p>"

  + "<p>"+'@string.Format(serviceDescription[i])'+"</p>"

  + "<p>"+'@string.Format("{0}", @Html.Raw(serviceDescription[i]))'+"</p>"

  + "<p>"+' @(new HtmlString(serviceDescription[i]))'+"</p>" 

  + "<p>"+' @Html.Raw(serviceDescription[i])'+"</p>"

  + ' @Html.Raw("<p>"+@serviceDescription[i]+"</p>")'

  "<p>" @Html.Raw(String.Format(serviceDescription[i]))+"</p>"

  + @(new HtmlString("<p>"+serviceDescription[i]+"</p>"))

我的整个 InfoWindow 代码:

  var info = new google.maps.InfoWindow({
            map: map
        });
        /*   <img src="data:image/jpeg;base64{binary data}" />   */


        google.maps.event.addListener(marker, 'click', function () {

            var $infoWindowContent = $("<div style='line-height:1.35;overflow:hidden;white-space:nowrap;width: 200px'><h3>" + '@serviceTitle[i].ToString()'
                + '</h3><img src=@string.Format("data:{0};base64,{1}", serviceImgMIME[i], Convert.ToBase64String(serviceImgBinary[i])) style="width: 100%;max-height: 100%"/>'


                + "<p>"+ @Html.Raw(String.Format("{0}",serviceDescription[i]))+"</p>" // this is the line that causes me pain
                + "Web: " + "<a href="+'http://@string.Format("{0}", serviceLink[i])'+">"+'@serviceLink[i].ToString()'+"</a>"
                + "<br>"
                + "Kontakt: "+ "<a href="+'mailto:@serviceContact[i].ToString()'+">"+'@serviceContact[i].ToString()'+"</a>"
                + "<p>"+"Lze platit v: "+"<b>"+'@serviceIDCryptoCur[i].ToString()'+"</b>"+"</p>"
                + @switch (serviceIDCryptoCur[i])
                  {
                      case "BTC": 
                <text>
                '<img height="20" width"20" src="~/Content/ikonky/btcSmall.png">'
            </text>
                          break;
                      case "LTC": 
                <text>
            '<img height="20" width"20" src="~/Content/ikonky/ltcSmall.png">'
            </text>
                          break;
                      case "BTC,LTC":
        <text>
            '<img height="20" width"20"  src="~/Content/ikonky/ltcSmall.png"> <img height="20" width"20"  src="~/Content/ikonky/btcSmall.png">' 
        </text>
                          break;
                      default:
        <text>
                '<img height="20" width"20"  src="~/Content/ikonky/btcSmall.png">'
        </text>
                          break;
                  }

                + "</div>");
            info.setContent($infoWindowContent[0]);
            info.open(map, this);
        });

最佳答案

您需要将此字符串生成移动到这样的函数

@functions{
    string infowindow(string title, string imgMime, string imgBase64, string descr, string link, string kontakt, string crypt){
        var result = @"<div style='line-height:1.35;overflow:hidden;white-space:nowrap;width: 200px'>
                <h3>{0}</h3>
                <img src='data:{1};base64,{2}' style='width: 100%;max-height: 100%' />
                <p>{3}</p>
                Web: <a href='{4}'>{4}</a><br />
                Kontakt: <a href='mailto:{5}'>{5}</a>
                <p>Lze platit v: <b>{6}</b></p>";
                switch (crypt)
                {
                    case "LTC":
                        result += "<img height='20' width='20' src='~/Content/ikonky/ltcSmall.png'>";
                    break;
                    case "BTC":
                        result += "<img height='20' width='20' src='~/Content/ikonky/btcSmall.png'>";
                    break;
                    case "BTC,LTC":
                        result += "<img height='20' width='20' src='~/Content/ikonky/ltcSmall.png'> <img height='20' width='20' src='~/Content/ikonky/btcSmall.png'>";
                    break;
                    default:
                        result += "<img height='20' width='20' src='~/Content/ikonky/btcSmall.png'>";
                    break;
            }
        result += "</div>";
        return result.Replace(Environment.NewLine,"");
    }
}

并像这样使用它

var $infoWindowContent = $("@Html.Raw(infowindow(serviceTitle[i],serviceImgMIME[i], Convert.ToBase64String(serviceImgBinary[i]),serviceDescription[i],serviceLink[i],serviceContact[i],serviceIDCryptoCur[i]))")

关于c# - 脚本1015 : Unterminated string constant when loading string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20273332/

相关文章:

python - 字符串中倒数第二次出现的整数 (Python)

c# - 为 iPhone 应用程序抓取 MS SQL Server 数据?

javascript - Jquery 测验 - 评分功能

c# - 如何提取 X509 证书智能卡

javascript - 使生成器递归函数

php - 如何在 Adob​​e AIR 应用程序中通过 JavaScript 将 XML 数据发布到 URL?

php - 为什么我的 die() 文本没有显示?

c# - String.IsNullOrEmpty 未检测到 C# 中的转义序列 NULL\0

c# - 当 API 返回 "too many requests"错误时,在 C# 中多次重试同一任务

c# - 如何在 Windows Phone 8 上使用 Windows.Storage 正确读写文件