NickHeidke NickHeidke - 3 months ago 59
C# Question

Chart.Mvc colliding with System.Web.Helpers.Chart

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;

Answer

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>

Comments