简体   繁体   English

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

[英]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.

相关问题 DNN 2sxc Razor模板-如果用户已通过身份验证 - DNN 2sxc Razor template - if user is authenticated 是否可以在 DNN 皮肤中加载特定的 2sxc 模块? - Is it possible to load a specific 2sxc module in a DNN skin? 从ADAM文件管理器获取2sxc模板中的DNN FileID - Getting DNN FileID in 2sxc Template from ADAM File Manager 2sxc-@foreach循环上的剃刀和JS之间的计算的var - 2sxc - Calculated vars between razor and JS on a @foreach loop 如何让 2sxc 应用程序 URL 显示在 DNN 的 Sitemap.aspx 中? - How can I make 2sxc app URLs show up in DNN's Sitemap.aspx? 是否可以通过 2sxc c# razor 模板加载 DNN 皮肤对象? - Is it possible to load DNN skin objects through 2sxc c# razor templates? DNN / 2SXC / Razor / c# / 使用可重用的 HttpClient 使用外部 API - DNN / 2SXC / Razor / c# / Consume External API with Reusable HttpClient 如何修复&#39;无法加载文件或程序集&#39;System.Web.Helpers,版本= 2.0.0.0&#39;错误与DNN中的2sxc - How to fix 'Could not load file or assembly 'System.Web.Helpers, Version=2.0.0.0' Error with 2sxc in DNN 创建2sxc内容模板 - Creating 2sxc Content Template 是否可以从 DNNSharp 的 Action Form 或 Action Grid 获取内容或令牌并将其显示在 2sxc 模板中? - Is it possible to get content or tokens from DNNSharp's Action Form or Action Grid and display it inside of a 2sxc template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM