2010-03-16 15 views
0

我有我使用的MSChart我的MVC項目時出現問題,當我使用項目的第一個索引頁,以渲染索引2的代碼部分視圖名稱是的MSChart:局部視圖錯誤

<% Html.RenderPartial("Index2"); %> 

但是,當我運行它的錯誤是發生該消息是

CS0029:無法隱式轉換類型「ASP.views_home_index2_ascx」到「System.Web.UI.Page」

- 它說,問題行代碼

://渲染圖表控制

第52行:Chart2.Page = this; < <在這裏

線53:HtmlTextWriter的作家=新 的HtmlTextWriter(Page.Response.Output);

第54行:Chart2.RenderControl(writer);

但是當我把所有的代碼Index2.ascx到的Index.aspx,而不是渲染局部視圖它做工精細

Index2.ascx的代碼是

<% 
      System.Web.UI.DataVisualization.Charting.Chart Chart2 = new System.Web.UI.DataVisualization.Charting.Chart(); 
      Chart2.Width = 412; 
      Chart2.Height = 296; 
      Chart2.RenderType = RenderType.ImageTag; 

      Chart2.Palette = ChartColorPalette.BrightPastel; 
      Title t = new Title("No Code Behind Page", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105)); 
      Chart2.Titles.Add(t); 
      Chart2.ChartAreas.Add("Series 1"); 

      Chart2.Series.Add("Series 1"); 

      // add points to series 1 
      Chart2.Series["Series 1"].Points.AddY(3); 
      Chart2.Series["Series 1"].Points.AddY(4); 
      Chart2.Series["Series 1"].Points.AddY(5); 

      Chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss; 
      Chart2.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105); 
      Chart2.BorderlineDashStyle = ChartDashStyle.Solid; 
      Chart2.BorderWidth = 2; 

      Chart2.Legends.Add("Legend1"); 

      // Render chart control 
      Chart2.Page = this; 
      HtmlTextWriter writer = new HtmlTextWriter(Page.Response.Output); 
      Chart2.RenderControl(writer); 

%>

回答

0

這個錯誤似乎表明它需要在ASP.NET頁面中,可能是因爲MVC體系結構的更改以及視圖如何不真正使用頁面/控件集合。

爲了簡化這個過程,您可以創建一個HTML助手方法,以可重用的方式爲您完成大部分工作。此外,您可以嘗試使用a,並將該控件嵌入頁面中作爲替代。如果你想做助手路線,你可以這樣做:

public static class ChartExtensions 
{ 
    public static string Chart(this HtmlHelper html, <settings>) 
    { 
    //Put code here, return a string 
    } 
} 

HTH。