C# .cshtml 文件中的 Razor - 添加额外的“if”语句会导致错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18908852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:33:57  来源:igfitidea点击:

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

c#razor

提问by Chris Halcrow

I'm trying to add a couple of if statements to a razor page. The page I'm changing worked before the changes however when I add the blocks I've highlighted in the code sample below I get this error:

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

Error loading Razor Script ~/macroscripts/genericpage.cshtml The code 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.

加载 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 *@                    
                @* ****************** *@

            }
      }
 }

I don't understand why the if statements inside the <div>are working however when I add the new (similar) statements outside the div I get the error.

我不明白为什么里面的 if 语句<div>正在工作,但是当我在 div 之外添加新的(类似的)语句时,我得到了错误。

****UPDATE****

****更新****

If I remove the @{} surrounding the if statements that I added, like this:

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

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

I see this on screen:

我在屏幕上看到这个:

The image you can see is the background applied to the div

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

采纳答案by riofly

When you need to open a HTML tag inside ifstatement but you don't want to close it, I suggest you to use @Html.Raw():

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

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

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

...then, when you close Atag...

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

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

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

回答by Martijn van der Put

You have an extra apostrophe and greater then sign after the last closing tag in the second if statement:

在第二个 if 语句的最后一个结束标记之后,您有一个额外的撇号和更大的 then 符号:

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

回答by Sanjay Maharjan

try using global @{} rather than using it in every section. The markup will work fine inside the razor syntax. In this way you can simplify your code.

尝试使用全局 @{} 而不是在每个部分都使用它。标记将在 razor 语法中正常工作。通过这种方式,您可以简化您的代码。