0👍
✅
Use JsonConvert.SerializeObject
along with Html.Raw
method to get a javascript array from the ViewBag item. You do not need to wrap it with [
and ]
as the result of the expression @Html.Raw(JsonConvert.SerializeObject(ViewBag.b))
will be an array.
var myLabels = @Html.Raw(JsonConvert.SerializeObject(ViewBag.b));
var myData = @Html.Raw(JsonConvert.SerializeObject(ViewBag.a));
//Let's verify it
console.log(myLabels);
console.log(myData);
var PieChartData =
{
labels : myLabels,
datasets: [{
label: 'ProductWise Sales Count',
backgroundColor: [
"#f990a7",
"#aad2ed",
"#9966FF",
"#99e5e5",
"#f7bd83",
],
borderWidth: 2,
data: myData
}]
};
Source:stackexchange.com