1

下面是在 ViewData 中返回数组的控制器操作。

  ViewData["arrayTotalCertificateCount"] = arrayTotalCertificateCount;
        ViewData["_CertificateCategory"] = _CertificateCategory;
        ViewData["arrayCodeSeries"] = arrayCodeSeries;
        ViewData["arrayCodeCounts"] = arrayCodeCounts;

        return PartialView();

现在,在 View, javascript 中,我无法访问它们。

下面是我用 View Javascript 编写的代码。

 var arrayTotalCertificateCount = new Array();
    var i =0;

    for(var item in <%= ViewData["arrayTotalCertificateCount"]%>)
    {    
        arrayTotalCertificateCount[i] = item;
        i=i+1;
    }

它会引发错误,例如-

 for(var item in System.String[])
Uncaught SyntaxError: Unexpected token ]

谁能帮我在javascript数组变量中转换这个ViewData数组?

4

1 回答 1

1

尝试这个

<% var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); %>
var arrayTotalCertificateCount = <%= serializer.Serialize(ViewData["arrayTotalCertificateCount"]) %>;
for(var item in arrayTotalCertificateCount )
{    
   arrayTotalCertificateCount[i] = item;
   i=i+1;
}
于 2013-07-02T06:30:15.600 回答