3

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.

2 Answers 2

3

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>
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can format the field name in the top of your for-loop:

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

Comments

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.