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;

2

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

2 Comments

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".
OK. I moved the reference to chart.js ABOVE the code in the page, and it now displays correctly.
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

1 Comment

You need reference to Chart.js as well
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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.