I have some code that is failing due to an error:
The name 'foo' does not exist in the current context
It's due to a variable scope issue that I'm confused about. I thought that this should work:
var foo = "<ul>";
@for (int i = 0; i < 10; i++)
{
foo += "<li>bar</li>";
}
foo += "</ul>";
The Razor syntax should invoke the for loop and the variable foo would still be in scope in terms of the javascript because by the time the browser interprets the code, the razor syntax is essentially invisible.
However, the error message I'm getting is from the compiler so somehow the C# is trying to reference foo. What am I missing and how do I modify the code so that I get the proper javascript code outputted so it concatenates <li>bar</li> like I'm attempting to do?