Here is my javascript and below is my C# code:
//javascript in view
var data = {
labels: [@Misc.printData(Model.chartLabels, true)],
datasets: [
{
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,1)",
data: [@Misc.printData(Model.chartData, false)]
}
]
}
//C# helper
public static IHtmlString printData(IEnumerable<string> data, bool putQuotes)
{
string str = String.Empty;
if (!data.Any())
return new HtmlString(str);
if (putQuotes)
str = @"""" + data.Aggregate((result, next) => result + @""", """ + next) + @"""";
else
str = data.Aggregate((result, next) => result + ", " + next);
return new HtmlString(str);
}
I am trying to populate a data structure in javascript with the information in a C# model passed to me by the controller. This is obviously an awful hack. Is there a better way to do it?