Melvyn Harbour

Software Developer living in Cambridge, England and working for Red Gate

Combining ASP.NET MVC and ASP.NET Charting Controls

As ScottGu recently posted on his blog, Microsoft have recently released a set of charting components. Several people followed up with comments on his post asking whether it was possible to use them with ASP.NET MVC. The good news is that it certainly is, if you are using ASP.NET as the View engine (the default). And there's actually relatively little that you have to do. This post outlines the steps you'll need to take to get it up and running.

You'll need to first add the references to the appropriate assemblies (System.Web.DataVisualization and System.Web.DataVisualization.Design if you want design support). You also need to add a couple of lines to your Web.Config file. In the <controls> section, you will need to add:

   1: <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
   2:  

You'll also need to add a new line to the httpHandlers section:

   1: <add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
   2:  

It's then pretty much simplicity itself to add a chart to a view page - you just drop it on as a server control exactly as in the examples in the sample project linked for Scott's post. You'll obviously need to call the code to insert the DataPoint objects into an appropriate Series, and this will probably lead to a few lines of code in the code behind file, but given that the data-transfer objects between the controller and the view are generally reasonably well structured, this code should be both readable, and fairly short, so I don't think that it's really a problem.

I think the best approach if you are interested in doing this is to simply have a go and see how it gets on. I think it's a nice simple solution myself and certainly doesn't represent a great deal of overhead to get the result out.

Comments

Web Development Community said:

You are voted (great) - Trackback from Web Development Community

# November 26, 2008 4:58 AM

EricTN said:

Great post!  Any chance you could throw together a small demo MVC .sln for download that already has this working for one chart?

# November 26, 2008 3:44 PM

Alex Gorev's Weblog said:

Melvin Harbour recently posted in his blog about using ASP.NET Chart control with ASP.NET MVC. Alex

# November 26, 2008 4:51 PM

HowTo: ASP.NET Chart Controls mit ASP.NET MVC nutzen | Code-Inside Blog said:

Pingback from  HowTo: ASP.NET Chart Controls mit ASP.NET MVC nutzen | Code-Inside Blog

# November 26, 2008 7:46 PM

HowTo: Use the new ASP.NET Chart Controls with ASP.NET MVC | Code-Inside Blog International said:

Pingback from  HowTo: Use the new ASP.NET Chart Controls with ASP.NET MVC | Code-Inside Blog International

# November 26, 2008 8:06 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# November 27, 2008 8:02 AM

nefajciar said:

Hi Melvyn,

does it mean that we can use any ASP.NET controls when we are using aspx view engine?

it certainly is no complicated to do so I wonder what "mess" will I produce once I start using these controls with MVC projects...

thanks

# November 27, 2008 8:44 AM

MelvynHarbour said:

@nefajciar

You certainly can use ASP.NET server controls, but since you've got no ViewState, many of them won't behave in quite the manner you might naively expect. Look at them in a classic ASP.NET project with ViewState turned off and you'll see what you would get. Of course, you're sacrificing a lot of the benefits of MVC (clean control of HTML), but I think something like the charting components is a good example of where you might choose to take that hit.

# November 27, 2008 9:35 AM

MelvynHarbour said:

I should also have pointed out that as you normally won't have a form with runat="server", the postback mechanism won't work, so lots of server controls also won't work quite as you expect. For example Button.

# November 27, 2008 11:06 AM

nefajciar said:

I see, thanks for the answers...

at least MVC architecture isn't so lost and with a little though there might appear nice controls in the future...

of course, we don't need controls for everything (button, listbox, textbox etc etc) but something like Captcha or Charts is nice to have in a simple form...

# November 29, 2008 6:58 AM

MelvynHarbour said:

@nefajciar,

I think charts and captcha are two rather different cases. Charts are almost entirely confined to the View, so you can quite easily justify a 'pre-packaged' control that you can use to display. A captcha is going to require server-side logic in order to work though, so you are going to need some code in the Controller layer to handle it. You can still have a pre-packaged control for it, but it's going to need to be split out into two parts, and possibly two assemblies. If there's interest, I might work up an example in a post?

# December 1, 2008 3:36 AM

John Miller » Blog Archive » Microsoft Charting Controls Based on Dundas Technology said:

Pingback from  John Miller  &raquo; Blog Archive   &raquo; Microsoft Charting Controls Based on Dundas Technology

# December 1, 2008 8:51 PM

Joel said:

For IIS7, the handler needs to be configured as

<add name="ChartHttpHandler" preCondition ="integratedMode" verb="GET,HEAD" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

# February 4, 2009 3:23 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)