I’m trying to return a chart to an MVC ActionResult as the view’s Model but am hitting the following error:
CS0012: The type
‘System.Web.UI.DataVisualization.Charting.Chart’
is defined in an assembly that is not
referenced. You must add a reference
to assembly
‘System.Web.DataVisualization,
Version=4.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35’.
The project I’m writing is in MVC3, using Razor as the front-end markup (which shouldn’t make any difference, right?). I’ve included the following declarations in my Web.Config
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<!-- Microsoft Chart Controls -->
<add name="ChartImg" path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="ReportViewerWebControl" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</handlers>
</system.webServer>
My ActionResult code is pretty vanilla:
[HttpGet]
public ActionResult Visits()
{
StatModel model = new StatModel();
return View(model);
}
And the view in question looks like this:
@foreach (Chart chart in Model.ColumnCharts)
{
@chart
}
From what I’m reading about the exception being returned, the problem is that the Chart type isn’t being picked up correctly by the view when it comes to rendering the image, but the System.Web.DataVisualisation assembly appears in my project references (v. 4.0.0.0). What else should I be looking at?
Is the type System.Web.UI.DataVisualization.Charting.Chart picked up from the GAC? It sometimes fails to load some assemblies from the GAC sometimes. Please add the corresponding dll in your bin directory directly and try running your app and see if that works out for you.
The problem might be due to the fact that you are referencing the type in your view and not in your controller, in that case you probably need to define the assembly info as a web.config setting or as a page directive.
Hope that fixes things