Wednesday, March 25, 2009

MVC Live Application Take 2

I've just released my second asp.net mvc application based on mvc 1.0!

Feel free to check it out at http://www.soccer.co.nz, feel free to check it out and let me know your thoughts.

UPDATE: SOCCER.CO.NZ has been sold.

If you want to see the website please visit http://soccer.co.nz.serv6.temphostspace.com

Thursday, March 12, 2009

Create a MVC Helper to Display a Microsoft Chart Control




1. Add a reference to C:\Program Files\Microsoft Chart Controls\Assemblies\System.Web.DataVisualization.dll and C:\Program Files\Microsoft Chart Controls\Assemblies\System.Web.DataVisualization.Design.dll

---Helper Class --

2. using System.Web.UI.DataVisualization.Charting;

3. Add static method.


public static void Chart(this HtmlHelper helper, string polltitle, List<int> values, List<string> labels, int width, int height, SeriesChartType ChartType, System.Web.UI.Page page)
{
System.Web.UI.DataVisualization.Charting.Chart Chart1 = new System.Web.UI.DataVisualization.Charting.Chart();
Chart1 = new System.Web.UI.DataVisualization.Charting.Chart();
Chart1.Width = width;
Chart1.Height = height;
Chart1.RenderType = RenderType.ImageTag;
// Populate series data
Chart1.Series.Add("Default");
Chart1.ChartAreas.Add("ChartArea1");
Chart1.Series["Default"].Points.DataBindXY(labels, values);

// Set Doughnut chart type
Chart1.Series["Default"].ChartType = ChartType;

// Set labels style
Chart1.Series["Default"]["PieLabelStyle"] = "outside";

Chart1.Titles.Add(polltitle);
// Enable 3D
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;

// Disable the Legend
//Chart1.Legends[0].Enabled = false;


// Render chart control
Chart1.Page = page;
HtmlTextWriter writer = new HtmlTextWriter(page.Response.Output);
Chart1.RenderControl(writer);


}


4. In your view page.



<%Html.Chart(ViewData["charttitle"].ToString(), (List<int>)ViewData["chartvalues"], (List<string>)ViewData["chartlabels"], 400, 400, SeriesChartType.Pie, this); %>


Obvisouly you could go nuts in your helper method and have all kind of different options like enable 3d, show title, color etc etc.

Enjoy.

Tuesday, March 10, 2009

It's Official I'm addicted to MVC Html Helpers

If you've been using MVC recently and haven't been using your own HTML helpers then I suggest you do!

I started off using HTML helpers very sparingly probably because I didn't understand them enough, however onced I'd started just "doing it" the more and more HTML Helpers I seem to create, they are easy to develop and makes your HTML view pages so much nicer and cleaner.

On my latest project I've gone absolutely nuts I have so many helpers its not funny (you can never have enough can you?). I've cut some of my HTML pages from a few hundred lines down to about 50 and because my website is so "modula" in design it makes it easy to plug in modules to different pages.

For example we have crap loads of google adsense on the site, we need to have different adsense for different pages so they fit within the allocated space etc. So at the moment we are storing all the adsense code in the database and then just using helpers to pull out the one we need.



And our MVC helper is:

public static string GoogleAdsense(this HtmlHelper helper, int AdsenseID)
{
SoccerDataContext s = new SoccerDataContext();
return s.tbl_settings_adsenses.Single(t => t.tbl_settings_adsense_id == AdsenseID).Description;
}

Probably my favourite helper at the moment is the "header" helper, basically every item on out website sites in a box with a colour header see below of example page with 4 headers.



My helpers then look like this, which I can change the colour with ease, saves have to copy and paste html code form one page to another.




<%=Html.StartHeader("Sponsored Links", "green", "") %>
<%=Html.GoogleAdsense(0) %>
<%=Html.EndHeader() %>


What are some of your favourite html helpers you have produced?