6

I have a cshtml partial view (Razor engine) that is used to render something recursively. I have two declarative HTML helper functions defined in this view and I need to share a variable between them. In other words, I want a view-level variable (not function-level variable).

@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='level-@level'>@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='level-@level'>@childCategory.Title</option>
        @RenderChildCategories(childCategory.Id);
    }
}

2 Answers 2

6

You can't do this. You will need to pass them as arguments to your helper functions:

@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='level-@level'>@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='level-@level'>@childCategory.Title</option>
        @RenderChildCategories(categories, level, childCategory.Id);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Are you sure? I mean, it's really ridiculous if we can't share variables. I think of Razor views as a scope and I think that variables could be defined in this scope. Though this answer helped me to do what I wanted to do, but I'm not sure about it. Thank you anyway for helping. :)
1

You can do this. View is just a class. You can easily declare a new field onto this class and use it anywhere in the code of your view:

@functions
{
    private int level = 0;
}

1 Comment

Done. Deleted Comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.