[英]if else statement is not being treated as code inside @foreach - 2sxc v11 DNN 9.8
I'm currently learning 2sxc and am building a directory app as my initial project.我目前正在学习 2sxc,并正在构建一个目录应用程序作为我的初始项目。
I'm trying to use the following code in a list view to change the way an item is displayed based on the Boolean "UpgradedListing"我正在尝试在列表视图中使用以下代码来更改基于 Boolean“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>
}
the resulting output looks like this:生成的 output 如下所示:
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
}
in other words the if else isn't being seen as code.换句话说, if else 不被视为代码。
Can any one explain why this is?谁能解释这是为什么?
You just need an @ symbol in front of the first if, so你只需要在第一个 if 前面有一个 @ 符号,所以
@if(listing.UpgradedListing == 'true'){
Also you've got a typo, your closing strong tag is missing its right >
另外你有一个错字,你的结束强标签丢失了它的权利
>
And 'true' is not the same as true (boolean).而且“真”与真(布尔值)不同。 2sxc will know and return a boolean for.UpgradedListing (if you have it set AS a boolean field... if you have it as a string, then you need
== "true"
2sxc 将知道并返回 boolean for.UpgradedListing (如果您将其设置为 boolean 字段...如果您将其作为字符串,那么您需要
== "true"
and you can also move the stuff that doesn't change outside the if/else to make it more readable...并且您还可以将不会更改的内容移到 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>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.