Chartjs-Get string[] in razor view javascript from mvc controller

3👍

The most easy way to do this is to insert the values in array.

<script>
var labels = [];
@foreach(var label in ViewBag.ChartLabel)
{
    <text>
        labels.push('@label');
    </text>
}
</script>

You can find here more ways to convert, the one that I think it’s the best

var labels = @Html.Raw(Json.Encode(ViewBag.ChartLabel));

Leave a comment