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: "Element 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With