177

I'm updating my old .aspx views with the new Razore view engine. I have a bunch of places where I have code like this:

<span class="vote-up<%= puzzle.UserVote == VoteType.Up ? "-selected" : "" %>">Vote Up</span>

Ideally I'd like to do this:

<span class="vote-up@{puzzle.UserVote == VoteType.Up ? "-selected" : ""}">Vote Up</span>

However there's two problems here:

  1. vote-up@{puzzle.UserVote .... is not treating the @ symbol as a start of a code block
  2. @puzzle.UserVote == VoteType.Up looks at the first part @puzzle.UserVote as if it's supposed to render the value of the variable.

Anyone know how to address these issues?

2
  • 6
    I haven't used Razor but based on what I'm seeing, try @(puzzle.UserVote == VoteType.Up ? "-selected" : "") Jan 22, 2011 at 21:18
  • 1
    As this is the top result for inline ternary operators in razor, I'll add that if your output contains html or encodable characters such as apostrophes, e.g. @(isSomething ? "class='test'" : "") for example injecting javascript or similar, it will encode them as entities like &#39; and break the page. So you must use Html.Raw(".."). Otherwise with the above code you'd end up with something like <p class=&#39;test&#39;> which is invalid.
    – NibblyPig
    Jun 25, 2019 at 7:48

4 Answers 4

334

This should work:

<span class="vote-up@(puzzle.UserVote == VoteType.Up ? "-selected" : "")">Vote Up</span>
1
  • 3
    very strange to use the paren rather then the brace Oct 1, 2017 at 17:03
55
@( condition ? "true" : "false" )
2
  • 2
    I went for this one, feels clean and is easy to read back later
    – Dan Harris
    Feb 6, 2018 at 16:17
  • 1
    If you need to display the value of a property (including a string) rather than calling ToString() - Which didn't work in my case. You can do this @(condition ? $"{foo.bar}" : "Default")
    – Dan Harris
    Feb 6, 2018 at 16:18
32

The key is to encapsulate the expression in parentheses after the @ delimiter. You can make any compound expression work this way.

10

In most cases the solution of CD.. will work perfectly fine. However I had a bit more twisted situation:

 @(String.IsNullOrEmpty(Model.MaidenName) ? "&nbsp;" : Model.MaidenName)

This would print me "&nbsp;" in my page, respectively generate the source &amp;nbsp;. Now there is a function Html.Raw("&nbsp;") which is supposed to let you write source code, except in this constellation it throws a compiler error:

Compiler Error Message: CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Web.IHtmlString' and 'string'

So I ended up writing a statement like the following, which is less nice but works even in my case:

@if (String.IsNullOrEmpty(Model.MaidenName)) { @Html.Raw("&nbsp;") } else { @Model.MaidenName } 

Note: interesting thing is, once you are inside the curly brace, you have to restart a Razor block.

4
  • Why not just @(String.IsNullOrEmpty(Model.MaidenName) ? Html.Raw("&nbsp;") : Model.MaidenName) Sep 26, 2018 at 8:59
  • 1
    Uhm.. how about because of the aforementioned Compiler Error ? ;) Oct 1, 2018 at 19:21
  • 4
    Sorry, I need more coffee :) Oct 2, 2018 at 13:22
  • 3
    You can avoid that error by making both sides of the ternary operator the same type, @(String.IsNullOrEmpty(Model.MaidenName) ? Html.Raw("&nbsp;") : Html.Raw(Model.MaidenName))
    – NibblyPig
    Jun 25, 2019 at 7:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.