60

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?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
raklos
  • 28,027
  • 60
  • 183
  • 301
  • Here is another set of answers: http://stackoverflow.com/questions/4091831/how-to-use-ternary-operator-in-razor-specifically-on-html-attributes – Chris Pitman Jan 02 '11 at 18:25
  • 1
    For your perusal: The brains behind razor syntax. This is very likely everything you ever wanted to know about razor and more: http://channel9.msdn.com/shows/Going+Deep/Andrew-Nurse-Inside-Razor/ – MrBoJangles Apr 09 '13 at 16:40

2 Answers2

123

You need parentheses to use an expression:

@(myPosts.Contains(item.ID)?"Yes":"No")
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
11

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

@(myPosts != null ? (myPosts.Contains(item.ID) ? "Yes" : "No") : "Null")
Eric K
  • 689
  • 9
  • 26
  • 1
    which is kind of use full when it comes to nullables: `@(myObject.NullableBool != null ? (myObject.NullableBool ? "Yes" : "No") : "What would I know..")` – Vegar Jun 17 '16 at 13:42