我编写了一个自定义 HTMLHelper 来输出一些 javascript。javascript 正在添加到页面中,但在页面加载时并未运行。如果我将生成的 JS 复制/粘贴到 Chrome 的控制台窗口中,则该脚本可以完美运行。我想做的事可能吗?
public static HtmlString CreatePieChart(this HtmlHelper helper, string divId, int width, Collection<PieChartSeriesItem> series)
{
if (width < 1)
{
throw new System.ArgumentException("Width of pie chart must be greater than zero");
}
StringBuilder htmlString = new StringBuilder();
htmlString.Append("<script type=\"type/javascript\">");
htmlString.Append("$(window).load(\"#");
htmlString.Append(divId);
htmlString.Append("\").kendoChart({");
htmlString.Append("title: { visible: false },");
htmlString.Append("chartArea: { background: \"transparent\", width: ");
htmlString.Append(width.ToString(CultureInfo.InvariantCulture));
htmlString.Append(" },");
htmlString.Append("legend: { visible: false },");
htmlString.Append("seriesDefaults: { labels: { visible: false } },");
htmlString.Append("series: [{");
htmlString.Append("type: \"pie\",");
htmlString.Append("padding: 0,");
htmlString.Append("overlay: { gradient: \"none\"},");
htmlString.Append("data: [");
htmlString.Append(CreatePieChartDataSeriesString(series));
htmlString.Append("]");
htmlString.Append("}],");
htmlString.Append("tooltip: { visible: true, template: \"#= category # (#= value #%)\"}");
htmlString.Append("});");
htmlString.Append("</script>");
return new HtmlString(htmlString.ToString());
}
这是页面中的调用
<div id="piechart">
@Html.Partial("_PieChart", Model.BalancePieChartData)
</div>