2012-03-30 105 views
0

从昨天开始,我尝试运行一个代码示例以显示FusionCharts和Mvc3 Razor的图表,但没有任何效果。请帮忙!!! :)使用Mvc3和Razor引擎渲染融合图表

这里是我做了什么:

  1. 我把谟从自由人。 here

  2. 之后,我使用项目转换器here将项目从Mvc2升级到Mvc3。

  3. 当我尝试运行代码(F5并在浏览器中显示结果)时,工作正常;所有图形都能正确显示。

如果我只是创建剃刀(.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相比,但非常令人沮丧。

如果您有任何想法,提前一个大的优惠。

回答

1

应该@Html.FChart("Chart01", ViewData["MyChart"], 600, 400)代替@{Html.FChart("Chart01", ViewData["MyChart"], 600, 400); }

+0

非常感谢。这解决了我的问题。我想知道为什么第二个不起作用。我认为第二个返回直接在http响应中的值,第一个返回视图处理的字符串,但为什么第一个工作? – Samuel 2012-04-02 14:55:13