0

I'm attempting to add Chart.Mvc controls (Chart.js) to my Asp.net MVC 5 website. However, when I follow the example here: https://github.com/martinobordin/Chart.Mvc

I'm receiving the following error:

CS0426: The type name 'Mvc' does not exist in the type 'System.Web.Helpers.Chart'

It appears the reference I have to Chart.Mvc is colliding with System.Web.Helpders.Chart.

I've tried it both with a fully qualified reference and without:

var barChart = new Chart.Mvc.ComplexChart.BarChart();
barChart.ComplexData.Labels.AddRange(new []{ "January", "February",  "March", "April", "May", "June", "July"});
barChart.ComplexData.Datasets.AddRange(new List<ComplexDataset>...

and

var barChart = new BarChart();
barChart.ComplexData.Labels.AddRange(new []{ "January", "February",  "March", "April", "May", "June", "July"});
barChart.ComplexData.Datasets.AddRange(new List<ComplexDataset>

This second option does throw a slightly different error:

CS0246: The type or namespace name 'BarChart' could not be found (are you missing a using directive or an assembly reference?)

Adding using Chart.Mvc; to the Controller or @Chart.Mvc to the View does not help;

Nick Heidke
  • 2,787
  • 2
  • 34
  • 58
  • Try using extern alias for the Chart.Mvc dll - see https://blogs.msdn.microsoft.com/ansonh/2006/09/27/extern-alias-walkthrough/ – potatopeelings Jun 04 '16 at 23:53
  • `extern alias` isn't supported by Razor views. http://stackoverflow.com/questions/17823342/external-razor-views-cant-see-external-models – Nick Heidke Jun 06 '16 at 14:26

3 Answers3

2

Ran into the same issue myself, and found the answer when poking through the Github repo. You need to add the namespaces to Chart.Mvc in the /Views/web.config file.

See the author's /Views/web.config file for reference.

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Routing" />
    <add namespace="Chart.Mvc.Sample" />
    <add namespace="Chart.Mvc" />
    <add namespace="Chart.Mvc.ComplexChart" />
    <add namespace="Chart.Mvc.SimpleChart" />
    <add namespace="Chart.Mvc.Sample.Models" />
    <add namespace="Chart.Mvc.Extensions"/>
  </namespaces>
</pages>

  • Did you ever get the chart to display? My View's config is the same as above except I don't have the two lines with "Sample" in them. No compilation error, but the Chart does not display. I've included chart.js in the "scripts" section. Debugging with Chrome I get... "Chart is not defined". – Rob L Jun 30 '17 at 02:50
  • OK. I moved the reference to chart.js ABOVE the code in the page, and it now displays correctly. – Rob L Jun 30 '17 at 02:51
0

Adding the reference in the Vies/web.config file helped to get rid of the compilation error, I still do not get the chart displayed on the web page

Douglas Acosta
  • 73
  • 1
  • 4
  • 7
0

Got it working and displaying by including the namespaces and adding the reference to Chart.js.

Also the namespaces can be included in the razor view directly.

@using Chart.Mvc;
@using Chart.Mvc.Extensions;
@using System.Web.Mvc;
@using System.Web.Mvc.Ajax;
@using System.Web.Mvc.Html;
@using System.Web.Optimization;
@using System.Web.Routing;
@using Chart.Mvc;
@using Chart.Mvc.ComplexChart;
@using Chart.Mvc.SimpleChart;


@Scripts.Render("~/scripts/Chart.js")
st_stefanov
  • 1,147
  • 1
  • 10
  • 28