2014-09-27 44 views
0

我想在我的mvc應用程序上顯示一些圖表,但我有一些錯誤。我在本地主機上開發。我有一個名爲ReportChart使用MVC,.net c顯示圖表#

@{ 
var myChart = new Chart(width: 600, height: 400) 
    .AddTitle("Chart Title") 
    .AddSeries(
     name: "Employee", 
     xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" }, 
     yValues: new[] { "2", "6", "4", "5", "3" }) 
    .Write(); 

}

和其他文件一個文件CSHTML使用該圖表:

<body> 
<h1>Chart Example</h1> 
<p>The following chart is generated by the <em>ReportChart.cshtml</em> file:</p> 
<p><img src="ReportChart.cshtml" alt="Cricketers" /> </p> 

唯一的問題是,網頁不會顯示任何圖片:/

+0

是我們應該知道ReportChart是什麼? – 2014-09-27 17:05:33

+0

@RobertHarvey我用這個網站按照我的代碼http://www.asp.net/web-pages/tutorials/data/7-displaying-data-in-a-chart – tiagocarvalho92 2014-09-27 17:15:31

+0

ReportChart.cshtml'顯示圖表如果你直接在瀏覽器中打開它? – 2014-09-27 17:20:08

回答

5

不,它不會在MVC中工作。在Razor視圖

//I CUT THE CODE WHERE I construct string[] t1 and int[] t2 these are just arrays 
    public ActionResult EfficiencyChart(string pid) { 



      var myChart = new Chart(width: 1000, height: 600) 
      .AddTitle("Employee's Efficiency") 
      .AddSeries(
       name: "Employee", 
       xValue: t2, 
       yValues: t1) 
      .Write(); 

      myChart.Save("~/Content/chart" + user.Id, "jpeg"); 
      // Return the contents of the Stream to the client 
      return base.File("~/Content/chart" + user.Id, "jpeg"); 
     } 

然後:你應該在控制器的操作方法創建圖表

<img src="@Url.Action("EfficiencyChart", "NAME_OF_THE_CONTROLLER", new { pid = @Model.Id })" /> 
+0

這是有道理的,我會嘗試.. – tiagocarvalho92 2014-09-27 17:59:47

+0

只是一個更多的問題,如果我想o查詢數據庫,我應該這樣做在我的控制器? – tiagocarvalho92 2014-09-27 18:00:48

+0

@ user2002274是的。如果你想從數據直接獲取數據(通過DbContext),它應該在控制器中完成。 – Yoda 2014-09-27 18:30:30

1

試試吧,

<p><img src="@Url.Action("ReportChart")" alt="Cricketers" /> </p> 

     public ActionResult ReportChart() 
      { 
       return PartialView(); 
      }