0
我看這裏的例子:@Html圖片助手顯示的圖像作爲HTML代碼
這個例子使用一個輔助使用剃刀來顯示一個谷歌的圖表圖像:
@Html.DrawChart("p", "40,60,40", "250x100", "Pie Chart")
這種方法組裝正確的代碼返回圖表:
<img src='http://chart.apis.google.com/chart?chs=250x100&chd=t:40,60,40&cht=p&chl=Pie Chart' />
但是在瀏覽器中顯示爲html代碼,而不是實際的圖像。如果我將幫助器輸出的完全相同的代碼複製到視圖中,它顯示正常。是否需要將@ Html.Helper的輸出更改爲代碼而不是文字字符串?
助手代碼:
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();
}
}
}
這正是我所需要的東西。謝謝! –