简体   繁体   English

错误“当前上下文中不存在”

[英]Error “Does not exist in current context”

I have the following code where depts has already been declared and is a query result of type IEnumerable<dynamic> 我有以下代码,其中已经声明了depts,并且是IEnumerable<dynamic>类型的查询结果

I am getting an error saying dept does not exist in the current context . 我收到一个错误消息,说dept does not exist in the current context If I comment out dept.Name there are no errors. 如果我注释掉dept.Name,则没有错误。

@{
  int I = 0;
  for(I=0; I < dropdowndepts.Length; I++){
    var depts = iStar.GetDepts() ;
    <li class="header header_@I">
      foreach(var dept in depts){
        <li>@dept.Name</li>
      }
    </li>
  }
}

OK new info, if i break up the loops then it compiles. 确定新信息,如果我破坏了循环,那么它将进行编译。 So the following works: 因此,以下工作原理:

@{
                    int I = 0;
                    string[] dropdowndepts = new string[6]{"Store Logo","Clothing","Footwear","Outerwear","Gear","Deals"} ;
                }
                    @for(I=0; I < dropdowndepts.Length; I++){
                        var depts = iStar.GetDepts() ;
                        @:<li class="header header_@I">
                            <ul>
                            @foreach(var dept in depts){
                                <li>@dept.Name</li>
                            }
                            </ul>
                        @:</li>
                    }

Your second foreach loop is being interpreted as markup. 您的第二个foreach循环被解释为标记。 Put an @ in front of it: 在其前面加上一个@:

@{
  int I = 0;
  for(I=0; I < dropdowndepts.Length; I++){
    var depts = iStar.GetDepts() ;
    <li class="header header_@I">
      @foreach(var dept in depts){
        <li>@dept.Name</li>
      }
    </li>
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM