9

I'm trying to use a ternary operator in Razor, similar to this question, but what I want to output contains whitespace. This code

@(selectedGoal == null ? "" : "value=" + selectedGoal.Name)

should produce

value="Goal 3"

as the value of selectedGoal.Name is "Goal 3". Instead, I get

value="Goal" 3

which is no good. I've tried a bunch of different combinations of escaped quotes, @ symbols and no @ symbols, and I just can't get this to work, i.e.

@(selectedGoal == null ? "" : "value=" + "selectedGoal.Name")
@(selectedGoal == null ? "" : "value=@selectedGoal.Name")

and then I just get something like

value="selectedGoal.Name"

Anyone know how this should be done?

Community
  • 1
  • 1
wohanley
  • 434
  • 5
  • 14
  • Nothing you are showing would produce those results. Clearly, the problem is in whatever sets the value of selectedGoal.Name. – Erik Funkenbusch May 16 '12 at 15:51
  • I use `selectedGoal.Name` elsewhere in the page and it works fine, no misplaced quotes or anything. – wohanley May 16 '12 at 16:00
  • Something is placing those quotes, and the code you have shown is not doing it. – Erik Funkenbusch May 16 '12 at 16:18
  • See http://stackoverflow.com/questions/3800473/how-to-concisely-create-optional-html-attributes-with-razor-view-engine/5490925#5490925 – Jonathan Moffatt Nov 11 '13 at 23:24
  • Does this answer your question? [How to use ternary operator in razor (specifically on HTML attributes)?](https://stackoverflow.com/questions/4091831/how-to-use-ternary-operator-in-razor-specifically-on-html-attributes) – Michael Freidgeim Aug 17 '21 at 02:24

2 Answers2

14

Your value attribute is missing its own quotes, so they are being automatically added before the space. Try moving value outside of the expression.

value="@(selectedGoal == null ? "" : selectedGoal.Name)"
Brandon
  • 68,708
  • 30
  • 194
  • 223
  • Comes out like this: `value=""Goal" 2"`. – wohanley May 16 '12 at 15:57
  • @wohanley, I'm not sure where those extra sets of quotes would come from. Does `.Name` actually contain the quotes as part of the string? – Brandon May 16 '12 at 16:00
  • It really, really shouldn't, but if this Razor is correct then maybe that's the only possibility. I'll triple-check. – wohanley May 16 '12 at 16:03
  • There should be nowhere that quotes are added to `Name`, and it's working fine (that is, quote-free) in other places on the same page. – wohanley May 16 '12 at 16:05
  • @wohanley, you're right try the updated answer. I think `@` is encoding the quotes again. My previous answer wrapped with `@Html.Raw` instead of just `@` seems to work, but this way is a little cleaner anyways. – Brandon May 16 '12 at 16:13
  • That works. I'd rather not have the empty value attribute when `selectedGoal == null`, but good enough is good enough. Thanks. – wohanley May 16 '12 at 16:18
  • @wohanley - the new version of Razor in MVC4 has a feature to remove empty attributes. You would need to add additional logic to do it in MVC3. – Erik Funkenbusch May 16 '12 at 16:20
0

What about

@(selectedGoal == null ? "" : "value=\"" + selectedGoal.Name + \")

Or you can try rendering them directly as an HTML block, using my method on Html literal in Razor ternary expression

Community
  • 1
  • 1
yoel halb
  • 12,188
  • 3
  • 57
  • 52