【问题标题】:Parsing error in Umbraco 7 partial view : missing closing brace despite all brace pairs being presentUmbraco 7 部分视图中的解析错误:尽管存在所有大括号对,但缺少右大括号
【发布时间】:2014-04-11 08:24:29
【问题描述】:

我目前正在努力掌握 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>  
}

【问题讨论】:

    标签: c# razor umbraco partial-views umbraco7


    【解决方案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>
        }
    }
    

    【讨论】:

    • 我的代码包含在 if 和 else 语句中。你的使用三元运算符。
    • 另外,当我用您提供的块替换代码时,我现在收到以下错误:System.FormatException:valueDictionary 的格式不正确并且缺少任何'id、nodeId、__NodeId ' 元素 (var cssClass = imageCount
    • 最终你使用三元运算符的解决方案更简洁,因此在这种情况下更好,但我仍然不明白为什么会出现这个 valueDictionary 错误。
    • 之前你有编译错误,现在你有一个运行时错误,它与 Umbraco.TypedMedia() 函数有关。 our.umbraco.org/forum/developers/api-questions/…。尝试转到 App_Data/Temp/ExamineIndexes 文件夹并从其中的索引文件夹中删除所有文件,这应该可以解决它。
    猜你喜欢
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多