Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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"

@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:

if(listing.UpgradedListing == 'true'){ Techmedics Ltd [email protected]
} else { Techmedics Ltd
[email protected]
}
if(listing.UpgradedListing == 'true'){ Solutions Online NZ Ltd [email protected]
} else { Solutions Online NZ Ltd
[email protected]
}

in other words the if else isn't being seen as code.

Can any one explain why this is?

like image 552
Martyn Cook Avatar asked Dec 14 '25 21:12

Martyn Cook


1 Answers

You just need an @ symbol in front of the first if, so

@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"

and you can also move the stuff that doesn't change outside the if/else to make it more readable...

@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>
}
like image 142
Jeremy Farrance Avatar answered Dec 16 '25 13:12

Jeremy Farrance



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!