2011-06-30 10 views
5

Mam częściowy widok cshtml (silnik Razor), który jest używany do renderowania czegoś rekursywnie. Mam dwie deklaratywne funkcje pomocnicze HTML zdefiniowane w tym widoku i muszę udostępnić zmienną między nimi. Innymi słowy, chcę zmienną na poziomie widoku (nie zmienną na poziomie funkcji).Jak zdefiniować zmienne poziomu widoku w 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='leve[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); 
    } 
} 

Odpowiedz

6

Nie możesz tego zrobić. Będziesz musiał przekazać je jako argumenty do funkcji pomocnika:

@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

Jesteś pewien? Mam na myśli, że to niedorzeczne, jeśli nie możemy udostępniać zmiennych. Myślę o poglądach Razora jako o zakresie i myślę, że można by zdefiniować zmienne w tym zakresie. Chociaż ta odpowiedź pomogła mi zrobić to, co chciałem zrobić, ale nie jestem tego pewien. Dziękuję ci za pomoc. :) –

0

Możesz to zrobić. Widok to tylko klasa. Możesz łatwo zadeklarować nowe pole na tej klasie i użyć go w dowolnym miejscu w kodzie widoku:

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

Zrobione. Usunięty komentarz. –

Powiązane problemy