raklos
raklos

Reputation: 28545

Shorthand if else with razor

Im using this in my view and want it to display only "Yes" or "No" but its displaying False?"yes":"No"

@myPosts.Contains(item.ID)?"Yes":"No"

Whats wrong here?

Upvotes: 61

Views: 43897

Answers (2)

Eric K
Eric K

Reputation: 716

You can even nest shorthand if inside of another shorthand if!

@(myPosts != null ? (myPosts.Contains(item.ID) ? "Yes" : "No") : "Null")

Upvotes: 11

Guffa
Guffa

Reputation: 700342

You need parentheses to use an expression:

@(myPosts.Contains(item.ID)?"Yes":"No")

Upvotes: 124

Related Questions