Shorthand if else with razor

C#asp.net MvcRazor

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

You need parentheses to use an expression:

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

Solution 2 - C#

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionraklosView Question on Stackoverflow
Solution 1 - C#GuffaView Answer on Stackoverflow
Solution 2 - C#Eric KView Answer on Stackoverflow