@{
int i = 0;
}
@helper Text() {
<input type="text" name="Ans[@i].Text" />
}
i
is not accessible in helper. How to access it?
@{
int i = 0;
}
@helper Text() {
<input type="text" name="Ans[@i].Text" />
}
i
is not accessible in helper. How to access it?
You can achieve this by changing base class for your view. This scenario applies to situation where helper is declared in view.
Create a base class that inherits from WebViewPage and introduce shared field or property:
Using
@inherits
directive change base class. And now field/property is acessible both from "page context" and helper:If you want to have a thing that is close to term "page property/field" but dont want to create a base class or helpers are stored within
App_Code
folder then you can try WebPageBase.Page property.The code in this case would be:
The drawback is that
Page
property is of typedynamic
and thus does not support intellisense. As an alternative toPage
there is another property - WebPageBase.PageData.In this case a class-container of strings/ints keys for "page variables" could be created. And the code would be like:
This at least provides constraints for shared page data but still no control over value type.
You could pass it as parameter to the helper:
and then:
or you could simply use editor templates which will take care of everything and get rid of those helpers. For example:
You can simply add it as member to you page by using @functions declaration: