@{ int i = 0; } @helper Text() { <input type="text" name="Ans[@i].Text" /> }
i
is not accessible in helper. How to access it?
Global Variable: The variable that exists outside of all functions. It is the variable that is visible from all other scopes. We can access global variable if there is a local variable with same name in C and C++ through Extern and Scope resolution operator respectively.
Create a function in your current working folder that sets the value of a global variable. function setGlobalx(val) global x x = val; Create a function in your current working folder that returns the value of a global variable.
The most common way of accessing global variables in ASP.net are by using Application, Cache, and Session Objects. Application – Application objects are application level global variables, that need to be shared for all user sessions. Thus, data specific to a user should'nt be saved in application objects.
You can simply add it as member to you page by using @functions declaration:
@functions { private int i; }
You could pass it as parameter to the helper:
@helper Text(int i) { <input type="text" name="Ans[@i].Text" /> }
and then:
@{ int i = 0; } @SomeHelper.Text(i)
or you could simply use editor templates which will take care of everything and get rid of those helpers. For example:
@Html.EditorFor(x => x.Ans)
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