剃刀:为什么我不变量的范围 [英] Razor: Why is my variable not in scope

查看:107
本文介绍了剃刀:为什么我不变量的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@inherits umbraco.MacroEngines.DynamicNodeContext
@using System.Collections;

@{ List<string> qa = new List<string>(); } //this is not defined in the recursive helper below

@helper traverseFirst(dynamic node){
   var items = node.Children.Where("umbracoNaviHide != true");
   foreach (var item in items) {
     foreach(var subItem in item.Descendants()) {
        if(subItem.Id == Model.Id)
        {
           qa.Add();
           break;
        }
     }
     @traverseFirst(item)
   }
}

@traverseFirst(@Model.AncestorOrSelf("Book"))

变量QA canot在递归助手进行访问。有没有办法解决?

The variable qa canot be accessed in the recursive helper. Is there a way around this?

推荐答案

定义变量在 @functions 部分。

正常 @ {放在你的代码中的一些方法体。使用 @functions 来定义类成员

The normal @{ places your code in some method body. Use @functions to define class members.

@functions{ List<string> qa = new List<string>(); } 



在这个问题上更多阅读:的SLaks解剖剃须刀系列。

这篇关于剃刀:为什么我不变量的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆