2015-11-24 74 views
0

我正在處理導航,出於某種原因,我無法增加變量。無法增加變量

<ul id="menu"> 
     <li> 
      <a href="/" class="drop">Home</a> 
      <div class="dropdown_2columns"> 
       <!-- Begin 2 columns container --> 
       <div class="col_2"> 
        <h2>Welcome !</h2> 
       </div> 
      </div><!-- End 2 columns container --> 
     </li><!-- End Home Item --> 
     @foreach (var mainNode in rootNode.Children()) 
     { 
      int childCount = 1; 
      int numChildren = mainNode.Children().Count(); 
      int num = 0;        @*<-------------- Num variable*@ 
      <li> 
       <a href="@mainNode.Url" class="drop">@mainNode.Name</a> 
       <div [email protected] class="dropdown_5columns"> @*<----------Using the variable here*@ 
        <!-- Begin 2 columns container --> 
        <div class="col_5"> 
         <h2>@mainNode.Name</h2> 
        </div> 
        @* note if you want ALL descendants change .Children to .Descendats*@ 
        @foreach (var childNode in mainNode.Children()) 
        { 
         // if first node or new set of three open the div and ul @: is used to stop razor trying to 
         // "balance" the tags 
         if (childCount == 1 || (double)childCount % 3 == 1) 
         { 
          @:<div class="col_1"> 
           @:<ul> 
         } 
         <a href="@childNode.Url">@childNode.Name</a> 

         // close the div and list if this is either a multiple of 3 or the last one 
         if ((double)childCount % 3 == 0 || numChildren == childCount) 
         { 
          @:</ul> 
         @:</div> 
         } 
         childCount++; 
         num++; @*< -----------Increasing the varable here*@ 
        } 
       </div> 

      </li> 
     } 
    </ul> 
} 

問題在於,循環後可變容量不會增加。甚至在循環多次後,div ID始終爲main-0。有誰知道這個的原因?

回答

1

「民」放在mainNode循環中,所以它總是設置爲0

你必須移動num變量的mainNode循環之外,它應該工作:-)

+0

太謝謝你了。 – John