ASP MVC 4.0 chart control rendering image properly

3.3k Views Asked by At

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

1

There are 1 best solutions below

2
On BEST ANSWER

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?