if else 语句未被视为 @foreach 中的代码 - 2sxc v11 DNN 9.8

if else statement is not being treated as code inside @foreach - 2sxc v11 DNN 9.8

我目前正在学习 2sxc 并且正在构建一个目录应用程序作为我的初始项目。

我正在尝试在列表视图中使用以下代码来更改基于布尔值“UpgradedListing”的项目显示方式

@foreach(var listing in AsList(Data)) {
<div @Edit.TagToolbar(listing)>
   if(listing.UpgradedListing == 'true'){
        <strong>@listing.ListingName</strong<br/>
        <a href='mailto:@listing.Email'>@listing.Email</a>
    <hr/>
    } else {
        @listing.ListingName<br/>
        <a href='mailto:@listing.Email'>@listing.Email</a>
    <hr/>
    }
</div>
}

结果输出如下所示:

if(listing.UpgradedListing == 'true'){ Techmedics Ltd office@techmedics.co.nz
} else { Techmedics Ltd
office@techmedics.co.nz
}
if(listing.UpgradedListing == 'true'){ Solutions Online NZ Ltd enquiries@solutions-online.co.nz
} else { Solutions Online NZ Ltd
enquiries@solutions-online.co.nz
}

换句话说,if else 不被视为代码。

谁能解释这是为什么?

你只需要在第一个if前面加一个@符号,所以

@if(listing.UpgradedListing == 'true'){

另外你有一个错字,你的结束强标签缺少它的权利>

和'true'不等同于true(布尔值)。 2sxc 会知道 return .UpgradedListing 的布尔值(如果你将它设置为布尔字段......如果你将它作为字符串,那么你需要 == "true"

您还可以将不会更改的内容移到 if/else 之外,以使其更具可读性...

@foreach (var listing in AsList(Data))
{
    // here you can still write C# without an @
    // because it's still in code-mode
    var x = 7; // this still works here
    <div @Edit.TagToolbar(listing)>
        <!-- here Razor switches to HTML because we had a Tag around it -->
        <!-- so we need to really introduce the code-mode again -->
        @if (listing.UpgradedListing)
        {
            <strong>@listing.ListingName</strong><br />
        }
        else
        {
            @listing.ListingName<br />
        }
        <a href='mailto:@listing.Email'>@listing.Email</a>
        <hr />
    </div>
}