asp.net-mvc – 如何在ASP.NET MVC中定义视图级变量?
发布时间:2020-12-16 07:03:42 所属栏目:asp.Net 来源:网络整理
导读:我有一个cshtml局部视图(Razor引擎),用于递归渲染.我在这个视图中定义了两个声明性 HTML帮助函数,我需要在它们之间共享一个变量.换句话说,我想要一个视图级变量(不是函数级变量). @using Backend.Models;@* These variables should be shared among function
我有一个cshtml局部视图(Razor引擎),用于递归渲染.我在这个视图中定义了两个声明性
HTML帮助函数,我需要在它们之间共享一个变量.换句话说,我想要一个视图级变量(不是函数级变量).
@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); } } 解决方法
你不能这样做.您需要将它们作为参数传递给辅助函数:
@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,childCategory.Id); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何从ActionExecutingContext获取MethodIn
- 如何从ASP.NET MVC中的某些操作的Authorize Attribute获取角
- asp.net – ScriptManager的EnablePartialRendering属性究竟
- asp.net-mvc-4 – 无法找到或加载已注册的.Net Framework数
- asp.net – 我应该买Obout控件吗?
- asp.net – 强制RadComboBox下拉方向
- asp.net – 在页面的css部分插入内联服务器标签的正确方法是
- asp.net – dotnet运行OR dotnet手表与开发环境从命令行?
- ASP.NET(C#)Web服务中的异常处理
- asp.net-mvc – 使用Razor,如何使所有“bool?”属性显示为
推荐文章
站长推荐
- asp.net-mvc-3 – 在开发和生产之间管理EF Code
- asp.net – 我可以愚弄HttpRequest.Current.Requ
- asp.net-mvc-3 – ASP.Net MVC隐藏/显示基于安全
- asp.net – 如何将SiteMap绑定到动态创建的TreeV
- asp.net – ‘检测到Autofac循环组件依赖性’错误
- asp.net-mvc – 如何忽略ASP.NET MVC 4中特定Vie
- ASP.NET mvc:如何使用今天的日期自动填充日期字
- asp.net-mvc – 为现有项目添加标识
- asp.net – 单击GridView行上的任意位置以进入编
- asp.net-mvc – Razor不会在HiddenFor中呈现隐藏
热点阅读