Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define view-level variables in ASP.NET MVC?

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);
    }
}
like image 767
Saeed Neamati Avatar asked Jun 30 '11 11:06

Saeed Neamati


People also ask

Can we use view state in MVC?

ASP.NET MVC does not use ViewState in the traditional sense (that of storing the values of controls in the web page). Rather, the values of the controls are posted to a controller method.

What is view in ASP.NET MVC?

A view is an HTML template with embedded Razor markup. Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET Core MVC, views are .cshtml files that use the C# programming language in Razor markup.

How do you define a variable in a Razor?

To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.

What is a view in ASP NET MVC?

A view is used to display data using the model class object. The Views folder contains all the view files in the ASP.NET MVC application. A controller can have one or more action methods, and each action method can return a different view.

What is the difference between views and controllers in MVC?

The Views folder contains all the view files in the ASP.NET MVC application. A controller can have one or more action methods, and each action method can return a different view. In short, a controller can render one or more views.

Does ASP NET MVC include a page?

For ASP.NET or Active Server Pages, ASP.NET MVC does not include anything that directly corresponds to a page. In an ASP.NET MVC application, there is not a page on disk that corresponds to the path in the URL that you type into the address bar of your browser. The closest thing to a page in an ASP.NET MVC application is something called a view.

What is the purpose of this MVC tutorial?

The purpose of this tutorial is to provide you with a brief introduction to ASP.NET MVC views, view data, and HTML Helpers. By the end of this tutorial, you should understand how to create new views, pass data from a controller to a view, and use HTML Helpers to generate content in a view.


2 Answers

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);
    }
}
like image 119
Darin Dimitrov Avatar answered Nov 15 '22 06:11

Darin Dimitrov


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;
}
like image 33
lorond Avatar answered Nov 15 '22 06:11

lorond