Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables from Razor to JavaScript?

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?

like image 275
user3062459 Avatar asked May 23 '17 18:05

user3062459


3 Answers

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: ""
             }
like image 60
Kumar Nitesh Avatar answered Sep 30 '22 05:09

Kumar Nitesh


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;
like image 26
Shan Avatar answered Sep 30 '22 04:09

Shan


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
    }
);
like image 34
George Findulov Avatar answered Sep 30 '22 04:09

George Findulov