Currently I used ternary expression following the post: Razor If/Else conditional operator syntax It works fine, but since in my case the expression values are pretty long I would like to write them in more clear manner, maybe using temporary variables on the way.
I know if
as such is a statement, not expression, nevertheless how to write it? The outcome should be just string (for displaying on the page).
So starting from:
@(condition ? string1 : string2)
how its if
counterpart would look like?
I searched and tried with writing if
s and lambdas as well, but every time I faced some syntax error. I would like to preserve this simplicity of returning string values, and not executing in each branch placing the value, like @Html.DisplayFor
.
Update:
My string
values are pure text, no html tags, or something fancy. Actual code just (a bit overcomplicated for this purpose, but I hope readable):
@(summary.Any() ? String.Join(", ", summary.Select(it => it.Key.ToString())) : "none")