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:
vote-up@{puzzle.UserVote
.... is not treating the @ symbol as a start of a code block@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?
The key is to encapsulate the expression in parentheses after the @ delimiter. You can make any compound expression work this way.
This should work:
In most cases the solution of CD.. will work perfectly fine. However I had a bit more twisted situation:
This would print me " " in my page, respectively generate the source
&nbsp;
. Now there is a functionHtml.Raw(" ")
which is supposed to let you write source code, except in this constellation it throws a compiler error:So I ended up writing a statement like the following, which is less nice but works even in my case:
Note: interesting thing is, once you are inside the curly brace, you have to restart a Razor block.