Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access global page variable in helper

@{     int i = 0; }  @helper Text() {     <input type="text" name="Ans[@i].Text" /> } 

i is not accessible in helper. How to access it?

like image 390
Sergey Metlov Avatar asked Oct 17 '12 20:10

Sergey Metlov


People also ask

How do you access global variables?

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.

How to write global variables in matlab?

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.

How can use global variable in ASP NET?

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.


2 Answers

You can simply add it as member to you page by using @functions declaration:

@functions {     private int i; } 
like image 111
Evgenyt Avatar answered Oct 29 '22 16:10

Evgenyt


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) 
like image 45
Darin Dimitrov Avatar answered Oct 29 '22 16:10

Darin Dimitrov