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);
}
}
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.
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.
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.
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.
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.
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.
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.
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);
}
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With