Tomáš Čičman
Tomáš Čičman

Reputation: 281

How to paste C# array to javascript array in asp.net razor

Currently, I have this code for copy data from my array in C# Model to javascript array.

var javascriptArray = [];
@foreach (var data in Model.Array)
{
     @:javascriptArray.push(@Html.Raw(data));
}

but in result html I have many times

javasriptArray.push("string1");
javasriptArray.push("string2");
javasriptArray.push("string3");
...

Is there any other way to do this?

thx

Upvotes: 0

Views: 1315

Answers (1)

Rafael Marcos
Rafael Marcos

Reputation: 252

You can try serializate the Array object (Newtonsoft.Json), example:

JsonConvert.SerializeObject(Model.Array);

Or you can print a var array in javascript:

var myArray = ["AA","BB","CC"];

Upvotes: 0

Related Questions