【问题标题】:Umbraco Razor Syntax IssueUmbraco Razor 语法问题
【发布时间】:2012-10-30 19:39:25
【问题描述】:

我让这个宏在实时服务器上愉快地运行。在 WebMatrix 中没问题。现在 WebMatrix 已将自身更新为 2(刷新),它不会运行一些宏,这是其中之一:-

    @{
  //Check there are slider image page loaded
  var theCount = @Model.Descendants("SliderImagePage").Count();

  if (theCount > 0)
    {
        foreach (var theImagePage in Model.Descendants("SliderImagePage"))
          {
           var theImage = theImagePage.Media("sliderImage","umbracoFile");
           if (theImagePage.IsFirst()) 
           {
            @:<div class="slide" style="background-image:url('@Html.Raw(theImage)');display:block;"></div>
            } else {
            @:<div class="slide" style="background-image:url('@Html.Raw(theImage)');display:none;"></div>
            }
          }    
     }
  else
  {
   @: No Picture Image pages set up
  }

}

它抱怨“:”在代码块的开头无效。

我在 VS2010 中有 MVC4 和 Razor 扩展。据我了解,这一切都是有效的。谁能解释为什么它不能通过验证?

谢谢。

【问题讨论】:

    标签: razor umbraco


    【解决方案1】:

    foreach 循环中的语句应该可以在不使用 @: 的情况下正常运行:

    @{
        //Check there are slider image page loaded
        var theCount = @Model.Descendants("SliderImagePage").Count();
    
        if (theCount > 0)
        {
            foreach (var theImagePage in Model.Descendants("SliderImagePage"))
            {
                var theImage = theImagePage.Media("sliderImage","umbracoFile");
                if (theImagePage.IsFirst()) 
                {
                    <div class="slide" style="background-image:url('@Html.Raw(theImage)');display:block;"></div>
                }
                else 
                {
                    <div class="slide" style="background-image:url('@Html.Raw(theImage)');display:none;"></div>
                }
            }    
         }
        else
        {
            @: No Picture Image pages set up
        }
    }
    

    【讨论】:

    • 谢谢,删除它们,但 VS2010 仍然抱怨 @: No Picture Image 行所以把它放在 @Html.Raw("Here");它似乎已经通过了。
    【解决方案2】:

    您似乎在 Razor 中发现了一个编译问题。有一个简单的解决方法;如果您从第 3 行中删除 '@' 字符,您上面提供的代码将在 Razor v1 和 Razor v2 中编译。

    为此,我在网页团队的 CodePlex 错误数据库中打开了一个错误,希望它会在下一个版本中得到解决。

    HTH, 粘土

    【讨论】:

    • 谢谢克莱。您的解决方法允许我修改所有宏并让它们工作,除了一个似乎是由于 html 开始和结束标签超出范围但我不知道该怎么做:
        @foreach(){ 代码在这里 }
      代码太长了,不能放在这里。
    • 嗨 Craig,为此,如果你使用:
        @foreach() { code }
      -- Gareth / agrath
    猜你喜欢
    • 1970-01-01
    • 2012-06-27
    • 2013-06-11
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多