[Chartjs]-Using data from model in Chart.js MVC c#

3👍

The code inside block @{ } is server side.
Other content of the script-tag is client side.

C# runs ToString() method when you use @listKeys/@listValues. It generates string like System.Collections.Generic.List[System.String] instead of content of the lists. You need to generate json objects instead.

Use @Html.Raw(Json.Serialize(listKeys)) instead of @listKeys to get correct json-object with data.

P.S. It’s not good practice to use a lot of server-logic inside views. You can remove your @{} block and get json for Model.PageViews.Keys and Model.PageViews.Values

0👍

It’s not good practice to use .NET variables in JavaScript code.
The right way is to create hidden html elements for values who you want to use in js function or use API that returns chart data as Json object.

Leave a comment