I was looking at the example here:
This example uses a helper to display a google graph image using razor:
@Html.DrawChart("p", "40,60,40", "250x100", "Pie Chart")
This method assembles the correct code to return the chart:
<img src='http://chart.apis.google.com/chart?chs=250x100&chd=t:40,60,40&cht=p&chl=Pie Chart' />
However in the browser this displays as html code, not the actual image. If I copy the exact same code that the helper outputs into the view, it displays fine. Is there anything needed to change the output of the @Html.Helper to code instead of a literal string?
Helper code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;
namespace SolarCars.Helpers
{
public static class GoogleChart
{
/// <summary>
/// Draws the chart.
/// </summary>
/// <param name="helper">The helper.</param>
/// <param name="chartType">Type of the chart.</param>
/// <param name="chartData">The chart data.</param>
/// <param name="chartSize">Size of the chart.</param>
/// <param name="chartLabel">The chart label.</param>
/// <returns></returns>
public static string DrawChart(this HtmlHelper helper, string chartType, string chartData, string chartSize, string chartLabel)
{
StringBuilder chartHtml = new StringBuilder("<img src='http://chart.apis.google.com/chart?chs=");
chartHtml.Append(chartSize);
chartHtml.Append("&chd=t:");
chartHtml.Append(chartData);
chartHtml.Append("&cht=");
chartHtml.Append(chartType);
chartHtml.Append("&chl=");
chartHtml.Append(chartLabel);
chartHtml.Append("' />");
return chartHtml.ToString();
}
}
}
If you return strings in an @ or <%: block the result will be automatically escaped. This was a (I think) a change between MVC1 and MVC2. The solution to this is to have your helper method return an instance of IHtmlString.
Then switch your return to return a new HtmlString