5

I have one C# variable "value" that I want to pass into JavaScript Chartjs data object. It renders the chart but does not include the two @p values. See code source below:

cshtml file:

@{
    int p1 = 43;
    int p2 = 45;


}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>     
    <div style="width: 400px;">
        <canvas id="lineChart" width="400" height="400"></canvas>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
    <script src="main.js"></script>
</body>
</html>

javascript file:

var chart = document.getElementById("lineChart");

var data = {
    labels: [2012, 2013, 2014, 2015, 2016, 2017],
    datasets: [
        {
            label: "My Chart Label",
            fill: false,
            lineTension: 0.1,
            data: ['(@p1)', '(@p2)', 50, 48, 47, 52]
        }
    ]
};

var lineChart = new Chart(chart,
    {
        type: 'line',
        data: data
    }
);

How can I write it so that it works?

4
  • 1
    Possible duplicate of Using Razor within JavaScript Commented May 23, 2017 at 18:50
  • 1
    Possible duplicate of passing server side mvc variables to javascript Commented May 23, 2017 at 18:59
  • 1
    Sorry, can you edit your answer for two variables @p1 and @p2? Commented May 23, 2017 at 19:08
  • 1
    Add p as a data attribute to lineChart element, e,g, data-p attrribute and get its value using jQuery : $('#lineChart').data('p') Commented May 23, 2017 at 19:09

3 Answers 3

9

You can do

<script type="text/javascript">
    var p1 = @p1, p2= @p2;
</script>

and use p1, p2.

  var data = {
        labels: [2012, 2013, 2014, 2015, 2016, 2017],
        datasets: [
            {
                label: "My Chart Label",
                fill: false,
                lineTension: 0.1,
                data: [p1, p2, 50, 48, 47, 52]
            }
        ]
    };

In this case you don't have to use hidden html fields and you can expand your code to use more field like

var tempObj ={
                   tempData: "",
                   otherData:"",
                   moreData: ""
             }
Sign up to request clarification or add additional context in comments.

1 Comment

One thing to add in is that any "@" that are part of the static javascript code ( as used in JSON+LD for schema.org) would need to be escaped as { "@@Key": "Value" }
8

Something like below

View:

@{
   var p1 = 43;
   var p2 = 45
}


<input type="hidden" id="PassingToJavaScript1" value=@p1>
<input type="hidden" id="PassingToJavaScript2" value=@p2>

JavaScript:

var p1 = document.getElementById('PassingToJavaScript1').value;
var p2 = document.getElementById('PassingToJavaScript2').value;

1 Comment

Sorry, can you edit your answer for two variables @p1 and @p2?
3

Use a hidden field which will hold the value and then read the hidden field's value from JavaScript.

Example:

@{
    int p = 43;

}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>     
    <div style="width: 400px;">
        <canvas id="lineChart" width="400" height="400"></canvas>
        <input type="hidden" id="someId" value="@p" />
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
    <script src="main.js"></script>
</body>
</html>

JS

var chart = document.getElementById("lineChart");
var hiddenFieldValue = document.getElementById("someId").value;

var data = {
    labels: [2012, 2013, 2014, 2015, 2016, 2017],
    datasets: [
        {
            label: "My Chart Label",
            fill: false,
            lineTension: 0.1,
            data: ['(hiddenFieldValue)', 45, 50, 48, 47, 52]  // Not really sure how to format the data but you get main idea...
        }
    ]
};

var lineChart = new Chart(chart,
    {
        type: 'line',
        data: data
    }
);

1 Comment

Sorry, can you edit your answer for two variables @p1 and @p2?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.