繁体   English   中英

.cshtml文件中的Razor-添加其他'if'语句会出错

[英]Razor in .cshtml file - adding additional 'if' statements gives error

我正在尝试将两个if语句添加到剃须刀页面。 我要更改的页面在更改之前可以工作,但是当我添加下面的代码示例中突出显示的块时,出现此错误:

加载Razor脚本〜/ macroscripts / genericpage.cshtml时出错,该代码块缺少结尾的“}”字符。 确保此块中所有“ {”字符都有一个匹配的“}”字符,并且没有“}”字符被解释为标记。

if (publishedRightHandPods.Any())
{
    foreach(var rh in publishedRightHandPods)
    {   
        if (rh.image.GetType() == typeof(DynamicXml))
        {               
            if (rh.link.GetType() == typeof(DynamicXml))
            {
                var linkSupplied = !String.IsNullOrEmpty(rh.link.url) && !String.IsNullOrWhiteSpace(rh.link.url);
                var titleSupplied = !String.IsNullOrEmpty(rh.title) && !String.IsNullOrWhiteSpace(rh.title);

                @* *************** *@
                @* I'M ADDING THIS *@                   
                @* *************** *@

                @{
                  if (linkSupplied) {
                    <a href="@(rh.link.url)">
                  }
                }

                @* ****************** *@
                @* END OF ADDED BLOCK *@                    
                @* ****************** *@

                   <div class="side-pod" style="background: url('@(rh.image.mediaItem.Image.umbracoFile)') no-repeat scroll 0 0 transparent; height: @(rh.image.mediaItem.Image.umbracoHeight)px">
                       @{
                           if (titleSupplied)
                           {
                               <h2 class="title" style="color: #@(rh.textColour);">@Html.Raw(rh.title.ToString())</h2>
                           }
                       }
                       @{
                           if (!String.IsNullOrEmpty(rh.linkText) && !String.IsNullOrWhiteSpace(rh.linkText) && !String.IsNullOrEmpty(rh.link.url) && !String.IsNullOrWhiteSpace(rh.link.url))
                           {
                               <a class="findoutmore-btn" href="@(rh.link.url)">@(rh.linkText)</a>
                           }
                       }
                   </div>

                @* *************** *@
                @* I'M ADDING THIS *@                   
                @* *************** *@

                @{
                  if (linkSupplied) {
                    </a>">
                  }
                }

                @* ****************** *@
                @* END OF ADDED BLOCK *@                    
                @* ****************** *@

            }
      }
 }

我不明白为什么<div>中的if语句起作用,但是当我在div之外添加新的(类似)语句时,出现了错误。

****更新****

如果删除添加的if语句周围的@ {},如下所示:

if (linkSupplied) {
    <a href="@(rh.link.url)">
}

我在屏幕上看到了这个:

您可以看到的图像是应用于div的背景

当您需要在if语句中打开HTML标记但又不想将其关闭时,建议您使用@Html.Raw()

@if (linkSupplied) { @Html.Raw(string.Format("<a href=\\"{0}\\">", rh.link.url)) }

然后,当您关闭A标签时...

@if (linkSupplied) { @Html.Raw("</a>") }

您在第二个if语句中的最后一个结束标记后加了一个撇号,然后加上了大于号,然后:

@{
                  if (linkSupplied) {
                    </a>">
                  }
                }

尝试使用全局@ {}而不是在每个部分中使用它。 该标记将在razor语法内正常工作。 这样,您可以简化代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM