Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6092363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:26:02+00:00 2026-05-23T12:26:02+00:00

I have a cshtml partial view (Razor engine) that is used to render something

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:26:02+00:00Added an answer on May 23, 2026 at 12:26 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a View (Products.cshtml), a Partial View (FeaturedProducts.cshtml), two Controllers (ProductsController and FeaturedProductsController),
I have a simple Wall.cshtml view that loads a _Search.cshtml Partial View that looks
I have one (razor) page that contain 5 different partial views. Each partial view
I currently have a partial view that is called within my _Layout.cshtml, and is
I have built an ASP.NET MVC 3 web application (with exlusively Razor/cshtml pages) that
I am using ASP.NET MVC 3, with the Razor engine. I have a partial
I have a partial view which consists of a div with a benner that
Let's say I have a javascript slide-show in a partial view... _Slideshow.cshtml: @{ ViewBag.Title
In Razor, when loading a partial view, it is possible to just specify the
I am testing code in a MVC HTML helper that throws an error when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.