0

I'm trying to make a Razor loop in Javascript. I've tried this code, but this wouldn't work. Nothing is displayed into the web-page.

Code:

$(function () {
        $('#container').highcharts({
            title: {
                text: 'Monthly Average Temperature',
                x: -20 //center
            },
            subtitle: {
                text: 'Source: WorldClimate.com',
                x: -20
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: {
                title: {
                    text: 'Temperature (°C)'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: '°C'
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: [
@{int i=0, compte = Model.statistique.Count;}
                @foreach (var item in Model.statistique) {
                    <text>
                        {
                            name: '@(item.nomProcessus)',
                            data: [@(item.lundi), @(item.mardi), @(item.mercredi), @(item.jeudi), @(item.vendredi)]
                        }
                    </text>
                    if (i < compte) {
                        @:,
                    }
                    else
                    {
                        @:]
                    }
                    i++;
                }
});
});

Original code:

jsFiddle

How do I to make the foreach razor loop works into it, please?

Any brilliant idea?

1
  • What's the problem you're facing here ? Commented Mar 8, 2014 at 20:07

1 Answer 1

1

Try this

   series: [
            @foreach(var item in Model)
            {
                @:{ name: '@item.nomProcessus', data: [@(item.lundi), @(item.mardi), @(item.mercredi), @(item.jeudi), @(item.vendredi)] },
            }
]

Note: You don't need to worry about that last coma. It is acceptable.

Sign up to request clarification or add additional context in comments.

8 Comments

You don't need that. I am assuming you are using it for the , right? It is acceptable to leave a coma in the last.
Glad to be of help buddy.
By the way, how to make a new line in JavaScript please?
try \r\n or \n Also read this
@item.lundi returns a double value with 12,5 as value. How do I please to make it appear as 12.5? Because this influence on the array by generating another cell.
|

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.