开发者

Local variables don't work in Razor helpers

开发者 https://www.devze.com 2023-02-07 18:51 出处:网络
This code does work inside Razor Views but does NOT work inside custom helpers body and in _Layout.cshtml:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消