從昨天開始,我嘗試運行一個代碼示例以顯示FusionCharts和Mvc3 Razor的圖表,但沒有任何效果。請幫忙!!! :)使用Mvc3和Razor引擎渲染融合圖表
這裏是我做了什麼:
如果我只是創建剃刀(.cshtml)一個新的觀點,而不是當前的.aspx(更換從舊的語法,新的剃刀視圖),我試圖顯示相同的圖形,網頁顯示正確但沒有圖形。當我用螢火蟲或任何其他工具查看頁面源代碼時,場景後面沒有代碼。在使用Firefox中的Web Developer工具查看時,我也沒有任何錯誤。
我只是試圖添加一個Html.Raw的代碼前生成的JavaScript不編碼輸出,我有相同的結果。還試圖返回HtmlString,但又是相同的結果;沒有圖形顯示。
不要錯過這個問題的關鍵是,如果我嘗試完全相同的代碼,但與.aspx文件,一切都是正確的。
在的.aspx,代碼如下所示:
而且在.cshtml:
@{Html.FChart("Chart01", ViewData["MyChart"], 600, 400); }
最後,HTML輔助生成該一堆代碼:
private static string RenderChart(string controlId, string xmlData, FusionChartBase chart, int width, int height)
{
String sControlId = controlId;
String sJsVarId = "_lib_JS_" + controlId;
String sDivId = "_lib_DIV_" + controlId;
String sObjId = "_lib_OBJ_" + controlId;
String sWidth = width.ToString();
String sHeight = height.ToString();
StringBuilder oBuilder = new StringBuilder();
oBuilder.AppendLine(@"<div id=""" + sDivId + @""" align=""center""></div>");
oBuilder.AppendLine(@"<script type=""text/javascript"">");
oBuilder.AppendLine(@"var " + sControlId + @" = (function() {");
oBuilder.AppendLine(@" return {");
oBuilder.AppendLine(@" containerId: '" + sDivId + "',");
oBuilder.AppendLine(@" xmlData: '',");
oBuilder.AppendLine(@" chartType: '',");
oBuilder.AppendLine(@" showChart: function() {");
oBuilder.AppendLine();
oBuilder.AppendFormat(@" var chartURL = '{0}' + this.chartType.replace('Chart', '{1}');", UrlSWF, SufixSWF);
oBuilder.AppendLine(@" var " + sJsVarId + @" = new FusionCharts(chartURL, """ + sObjId + @""", """ + sWidth + @""", """ + sHeight + @""");");
oBuilder.AppendLine(@" " + sJsVarId + @".setDataXML(this.xmlData);");
oBuilder.AppendLine(@" " + sJsVarId + @".render(""" + sDivId + @""");");
oBuilder.AppendLine(@" }");
oBuilder.AppendLine(@" }");
oBuilder.AppendLine(@"})();");
oBuilder.AppendLine(@"setTimeout(function(){");
oBuilder.AppendLine(@" " + sControlId + @".xmlData = """ + xmlData.Replace(@"""", @"'") + @""";");
oBuilder.AppendLine(@" " + sControlId + @".chartType = """ + chart.ChartType + @""";");
oBuilder.AppendLine(@" " + sControlId + @".showChart();");
oBuilder.AppendLine(@"},0);");
oBuilder.AppendLine(@"</script>");
return oBuilder.ToString();
}
我不知道我是否必須在web.config中設置一些配置選項或者我不瞭解的行爲Mvc2與Mvc3相比,但非常令人沮喪。
如果您有任何想法,提前一個大的優惠。
非常感謝。這解決了我的問題。我想知道爲什麼第二個不起作用。我認爲第二個返回直接在http響應中的值,第一個返回視圖處理的字符串,但爲什麼第一個工作? – Samuel 2012-04-02 14:55:13