我正在将一个C#字符串数组传递到使用TagBuilder构建一些Javascript的方法中。 我似乎无法在我的Javascript中获取实际的数组项:
public static IHtmlString NewsTicker(this HtmlHelper htmlHelper, string[] arrTickerContents)
{
TagBuilder script = new TagBuilder("script");
script.Attributes.Add("type", "text/javascript");
script.InnerHtml = @"
var rss_scr_contents = new Array();
$.each('" + arrTickerContents + @"', function (i, objValue) {
rss_scr_contents[i] = objValue;
});
return MvcHtmlString.Create(script.ToString());
}
这将导致以下源代码:
var rss_scr_contents = new Array();
$.each('System.String[]', function (i, objValue) {
rss_scr_contents[i] = objValue;
});
这要怎么做才是正确的语法?
int[]
一样受限制,因此它可能会在给定输入的许多(也许是大多数)值中出现问题。我建议只使用“to JSON”。 - user166390$.each('[' + string.Join(',', arrTickerContents) + @']', function (i, objValue) {
。但是即使你这么做了,如果你的输入数组是 ["one", "two", "three"],那么结果将会是$.each('['"one, two, three"']', function (i, objValue) {
,对吗? - Scott MermelsteinarrTickerContnets)
后面的逗号应该导致编译错误。 - Scott Mermelstein$.each('[' + string.Join(',', arrTickerContents) + @']', function (i, objValue) {
,我相信它会产生$.each('['one, two, three']', function (i, objValue) {
。我认为在定义JavaScript数组时,每个字符串都需要有''或""。 - Scott Mermelstein