This code does work inside Razor Views but does NOT work inside custom helpers body and in _Layout.cshtml:
@{ var v = "foo"; }
@v
Error message: "Elem开发者_运维问答ent v does not exist in current context"
I use just downloaded Visual Web Developer Express 2010.
Can anybody explain this behaviour?
This code works fine in both views and _layout.cshtml
. And as far as helpers are concerned here's the syntax:
@helper MyHelper()
{
var v = "foo";
@v
}
@MyHelper()
This being said declaring local variables and writing C# code in views should be avoided.
精彩评论