0

I'm trying to get a System.Web.UI.Datavisualization.Chart control to render on my page using ASP MVC 4.0 (.net 4.5 on VS 2012 RC). I realize it isn't easy/standard practice to use the webforms controls on MVC page Views directly so I'm trying to use an action in the controller to return an image of the chart that the View can display.

In the HomeController I have:

public FileResult DataChart()
{
        Chart newChart = new Chart()
        {
            Width = 500,
            Height = 300
        };
        List<double> DataPoints = new List<double>();
        new LogModelContainer().Measurements.Select(i => i.MU).ToList().ForEach(i => DataPoints.Add(i));
        Series newSeries = new Series()
        {
            ChartType = SeriesChartType.Bar
        };
        newSeries.Points.Add(DataPoints.ToArray());
        newChart.Series.Add(newSeries);
        newChart.ChartAreas.Add(new ChartArea());
        var returnVal = new MemoryStream();
        newChart.SaveImage(returnVal);
        return File(returnVal.GetBuffer(), @"image/png");
 }

Note: the data for the chart is coming from an EF 5 model linked to a SQL database (the data does actually exist and load properly).

I'm trying to use it from the Index.cshtml as follows:

          <img src="/HomeController/DataChart" alt="" />  

This seems equivalent to me of what is used in the example on CodePlex(MVC Chart Control Example CodePlex I found on the web other than that I don't require that a parameter be passed to my action.

All I get when I run the code is the broken image icon on the page where this chart should be. I'm a complete ASP noobie (this is my first project using the technology) so it is quite possible I'm doing something stupid. Can anyone tell me what's wrong? Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
Mike
  • 418
  • 3
  • 10

1 Answers1

1

Wouldn't the img src be /Home and not /HomeController? (Not sure if they're equivalent but I've always used just the controller name without appending the word 'controller'). That being said, look at the front-end using developer tools in Chrome/FF and see what the image source is actually being rendered as?

Code Monkey
  • 643
  • 6
  • 18
  • Yep that fixes it. Just changing it to /Home worked. This is confusing to me why is your "Home" page called Index in the View, but HomeController in controllers etc? Admittedly I'm a noobie but I wouldn't expect to have to take things off of class names to get asp to make it presumably in the backend somehow magically add the Controller part to it to find it in the right class again. I don't have rights to vote up yet can someone/s give this answer a poke it solved it. Thanks Ashish. – Mike Aug 23 '12 at 21:51
  • 1
    Those are MVC "conventions" (which btw I believe you can override if needed), you can change the view name from Index to XYZ and say return XYZ() as the return for your controller. That ends up becoming your HTML page name. The "magic" is configured as part of MVC routing (look at the global.asax file in your project). The "Home" controller returns the "Index" view in your scenario. – Code Monkey Aug 23 '12 at 22:33