2011-06-30 41 views
5

Tengo una vista parcial cshtml (motor Razor) que se utiliza para representar algo recursivamente. Tengo dos funciones declarativas de ayuda HTML definidas en esta vista y necesito compartir una variable entre ellas. En otras palabras, quiero una variable de nivel de vista (no variable de nivel de función).¿Cómo se definen las variables de nivel de vista en ASP.NET MVC?

@using Backend.Models; 
@* These variables should be shared among functions below *@  
@{ 
    List<Category> categories = new ThoughtResultsEntities().Categories.ToList(); 
    int level = 1; 
} 

@RenderCategoriesDropDown() 

@* This is the first declarative HTML helper *@ 
@helper RenderCategoriesDropDown() 
{ 
    List<Category> rootCategories = categories.Where(c => c.ParentId == null).ToList(); 
    <select id='parentCategoryId' name='parentCategoryId'> 
    @foreach (Category rootCategory in rootCategories) 
    { 
     <option value='@rootCategory.Id' class='[email protected]'>@rootCategory.Title</option> 
     @RenderChildCategories(rootCategory.Id); 
    } 
</select> 
} 

@* This is the second declarative HTML helper *@ 
@helper RenderChildCategories(int parentCategoryId) 
{ 
    List<Category> childCategories = categories.Where(c => c.ParentId == parentCategoryId).ToList(); 
    @foreach (Category childCategory in childCategories) 
    { 
     <option value='@childCategory.Id' class='[email protected]'>@childCategory.Title</option> 
     @RenderChildCategories(childCategory.Id); 
    } 
} 

Respuesta

6

No puede hacer esto. Tendrá que pasarlos como argumentos a sus funciones auxiliares:

@using Backend.Models; 
@{ 
    List<Category> categories = new ThoughtResultsEntities().Categories.ToList(); 
    int level = 1; 
} 

@RenderCategoriesDropDown(categories, level) 

@helper RenderCategoriesDropDown(List<Category> categories, int level) 
{ 
    List<Category> rootCategories = categories.Where(c => c.ParentId == null).ToList(); 
    <select id='parentCategoryId' name='parentCategoryId'> 
    @foreach (Category rootCategory in rootCategories) 
    { 
     <option value='@rootCategory.Id' class='[email protected]'>@rootCategory.Title</option> 
     @RenderChildCategories(categories, level, rootCategory.Id); 
    } 
    </select> 
} 

@helper RenderChildCategories(List<Category> categories, int level, int parentCategoryId) 
{ 
    List<Category> childCategories = categories.Where(c => c.ParentId == parentCategoryId).ToList(); 
    @foreach (Category childCategory in childCategories) 
    { 
     <option value='@childCategory.Id' class='[email protected]'>@childCategory.Title</option> 
     @RenderChildCategories(categories, level, childCategory.Id); 
    } 
} 
+0

¿Estás seguro? Quiero decir, es realmente ridículo si no podemos compartir variables. Pienso en los puntos de vista de Razor como un ámbito y creo que las variables podrían definirse en este ámbito. Aunque esta respuesta me ayudó a hacer lo que quería hacer, pero no estoy seguro de eso. Gracias de todos modos por ayudar. :) –

0

Puede hacerlo. La vista es solo una clase. Puede declarar fácilmente un nuevo campo en esta clase y usarlo en cualquier lugar del código de su vista:

@functions 
{ 
    private int level = 0; 
} 
+0

Hecho. Comentario eliminado. –

Cuestiones relacionadas