Ravior Ravior - 21 days ago 13
C# Question

Razor: Write to website

I want to build up a javascript array using razorcode. The final javascript code should look like this:

var data = [[10, 20], [30, 40]];


So I think I should do something like this:

var data = [
@foreach (Data d in Model.data)
{
[d.a, d.b]
}
];


However the central [d.a, d.b]: C# compiler thinks I want to do something in c#, but I simply want the [ ] and the , written to the website at that position, including the values from the model that Ive adressed there.

How do I do that?

Answer
var data = [
    @foreach (Data d in Model.data)
    {
            @Html.Raw("[d.a, d.b]")
    }
];
Comments