Chartjs-ChartJs Uncaught ReferenceError for labels value from ViewBag

1👍

Your plugin expects an array of values, but your passing it a string by using String.Join().

Pass the array using

ViewBag.SaleCount_List = salesCount;
ViewBag.TeamName_List = teamsList;

(or better pass a view model with 2 IEnumerable<string> properties) and then convert it to a jacascript array

var saleCounts = @Html.Raw(Json.Encode(ViewBag.SaleCount_List))
var teamNames = @Html.Raw(Json.Encode(ViewBag.TeamName_List))
var barChartData =
{
    labels: teamNames,
    datasets: [{
        ....
    ],
    borderWidth: 2,
    data: saleCounts
    }]
};

0👍

Using your current syntax:

    const string quote = "\"";
    foreach (var team in Db.Teams)
    {
        teamsList.Add(quote + team.Name + quote);
        int count = Db.LeadCampaigns.Count(i => Db.Agents.FirstOrDefault(a => a.AgentId == i.AgentId).TeamId == team.TeamId && i.LeadStatusId == Db.LeadStatuses.FirstOrDefault(s => s.Name == "SALE").LeadStatusId);
        salesCount.Add(count.ToString());
    }

Leave a comment