Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

razor syntax within quoted javascript

How do I escape my javascript so I can perform the following?

This is an example:

It's not outputting 'Month1', 'Month2' etc. and instead it is actually outputting 'Month@i'.

     @for (int i = 0; i < 12; i++)
       { 
           <text>
           {
               field: 'Month@i',
               title: ,
               width: 10
           },
           </text>
       }

Thanks.

like image 974
Mike Avatar asked Jun 17 '26 02:06

Mike


1 Answers

The main problems is that Month@i is wrongly interpreted as an email address, so you need to use explicit expressions @(...) to tell the Razor that this is a code expression. This is the best solution:

@for (int i = 0; i < 12; i++)
{ 
    <text>
    {
        field: 'Month@(i):',
        title: ,
        width: 10
    },
    </text>
}
like image 82
Nikolay Kostov Avatar answered Jun 19 '26 16:06

Nikolay Kostov