Umbraco 7局部视图中的解析错误:尽管存在所有支撑对,但缺少关闭支撑

时间:2014-04-11 08:24:29

标签: c# razor umbraco partial-views umbraco7

我正在努力尝试使用Umbraco并使用Razor但是我发现Razor代码的内联特性特别困难。出于某种原因,当我编译下面的部分视图(它将一个Bootstrap轮播输出到调用它的页面上的屏幕)时,我收到以下编译错误:

Line 3:  @if (Model.Content.HasValue("bannerImages")){  
Parser Error Message: The if block is missing a closing "}" character.  Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.

但是我已经在Notepad ++中检查了所有标签(因为这样可以更好地识别Visual Studio中的匹配标签)并且我文档中的所有大括号都会占用,所以这让我相信我可能会实现一些内联Razor代码块不正确。有什么想法吗?

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@if (Model.Content.HasValue("bannerImages")){   
    var bannerImagesList = Model.Content.GetPropertyValue<string>("bannerImages").Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
    var bannerImagesCollection = Umbraco.TypedMedia(bannerImagesList).Where(x => x != null);
    var imageCount = 0;

    <div id="carousel" class="carousel slide" data-ride="carousel">
      <!-- Indicators -->
      <ol class="carousel-indicators">
        <li data-target="#carousel" data-slide-to="0" class="active"></li>
        <li data-target="#carousel" data-slide-to="1"></li>
        <li data-target="#carousel" data-slide-to="2"></li>
        <li data-target="#carousel" data-slide-to="3"></li>
      </ol>

      <!-- Wrapper for slides -->
      <div class="carousel-inner">
        @foreach (var bannerImage in bannerImagesCollection){
            if (@imageCount < 1){
                <div class="item active">
            }else{
                <div class="item">
            }
                    <img src="@bannerImage.Url" alt="@bannerImage.Id" />
                </div>
        }
      </div>
    </div>  
}

1 个答案:

答案 0 :(得分:1)

您的问题出在代码的foreach部分。您的div需要包含在if和else语句中。

所以要取代你的foreach:

@foreach (var bannerImage in bannerImagesCollection)
{
    if (@imageCount < 1){
        <div class="item active">
    }else{
        <div class="item">
    }
        <img src="@bannerImage.Url" alt="@bannerImage.Id" />
        </div>
    }
}

有了这个,它将起作用:

@foreach (var bannerImage in bannerImagesCollection)
{
    var cssClass = imageCount < 1 ? "item active" : "item";

    <div class="@cssClass">
        <img src="@bannerImage.Url" alt="@bannerImage.Id" />
    </div>
}

编辑(使用if else)

@foreach (var bannerImage in bannerImagesCollection)
{
    if (imageCount < 1)
    {
        <div class="item active">
            <img src="@bannerImage.Url" alt="@bannerImage.Id" />
        </div>
    }
    else
    {
        <div class="item">
            <img src="@bannerImage.Url" alt="@bannerImage.Id" />
        </div>
    }
}