0

I have this:

<script>var questions = ['A', 'B', 'C'];</script>

And I want something like from that to this:

<script>var questions = [@foreach (var word in Word) { word.abc; } ]</script>

That mistake because I don't know how using '' in Razor

  • Pls take a look. Similar question and [answer](https://stackoverflow.com/questions/23781034/razor-mvc-populating-javascript-array-with-model-array) – Aziz Umarov May 16 '20 at 23:01

2 Answers2

1

Since you are using razor, you can use this.

<script>
    @{ //example
        var Word = new string[]{ "A", "B", "C" };
    }

    var questions = [@Html.Raw($"'{string.Join("','", Word)}'")];
</script>
VhaL
  • 26
  • 3
0

Use <text> block to transfer raw strings to js.

<script>
var questions = [];
@foreach (var word in Word) { 
    <text>
  questions.push('@word.abc');
    </text>
} 
</script>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Li-Jyu Gao
  • 932
  • 6
  • 9