SCRIPT1015:加载字符串时未终止的字符串常量

时间:2013-11-28 19:11:11

标签: c# javascript string format

您好我的ASP.NET Razor v2 cshtml文件中有这个代码,应该将string加载到此段落中,变量来自c# list of strings但是当加载的string包含类似的内容时这样:

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

我收到此JavaScript严重错误:SCRIPT1015: Unterminated string constant

该程序失败,因为某些字符已转义ending quotation mar '

实际上代码看起来像这样:

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

serviceDescription是可能包含任何可以逃避'字符的值的字符串列表。

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

此代码用于调整InfoWindow in Google API

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

  + "<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);
        });

1 个答案:

答案 0 :(得分:2)

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

@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]))")