开发者

Umbraco Razor newbie syntax issue

I have the following code block that nearly works. It completes the outer @foreach cycle and starts each sub @foreach cycle. It puts out the first image in each pair but then outputs

"} if(countItem==1) (" which can be read in the browser.

Any advice would be appreciated

Craig

Code:-

@inherits umbraco.MacroEngines.DynamicNodeContext

@{     
    int level = 2;
    var subjects = @Model.AncestorOrSelf(level).Children.Where("nodeTypeAlias == \"SideGallerySectionHeading\"");
    int countItem = 1;

    if (subjects != null) {
      <div class="highslide-gallery">
          @foreach(var subjectName in subjects){
          <h3>@subjectName.Name</h3>           
          foreach(dynamic image in subjectName.Children) {
              if(countItem==1){<div class="picrowdiv">}
              <div class="picdiv">
                  <a href="@image.Media("itemLarge","u开发者_开发知识库mbracoFile")" class="highslide"     onclick="return hs.expand(this)">
                  <img src="@image.Media("itemThumbnail","umbracoFile")" width="100"     height="100" alt="test"></a>
                  <div class="highslide-caption">
                    @image.bodyText
                  </div>
                  <p>@image.caption</p>
              </div>
              if(countItem==1){</div>}
              countItem++;                                                
              if(countItem>2){countItem=1;}                
            }
          }
      </div>
    }
 }


You may try to rewrite the line

if(countItem==1){</div>}

into

@if(countItem==1){
    </div>
}


Razor is quite picky about closing your html tags so you may have to refactor to make this work. I like ternary operators for stuff like this

try

@(countItem==1 ? "</div>")
@countItem++;
@(countItem>2 ? countItem=1) 

you can also do else with a :

so

@(countItem==1 ? "</div>" : "")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜