0

I've looked around for a while for an answer but no one has one and everyone keeps saying it's how it's supposed to work. I need to render an & (ampersand) as plain javascript code. Not as a string.

if (@(Model.Month == null ? "now.getMonth() == tooltipItems[0].index" : "now.getMonth() == tooltipItems[0].index && now.getDay() == tooltipItems[0].index") && now.getFullYear() == $('#DistinctYears').val()) {
   //
} else {
   //
}

I need this section:

"now.getMonth() == tooltipItems[0].index && now.getDay() == tooltipItems[0].index"

To render as plain javascript code but when it renders, the ampersands automatically get converted to && enter image description here

Aquaphor
  • 129
  • 10

1 Answers1

2

Surround the entire ternary expression with Html.Raw():

@Html.Raw(Model.Month == null ? "now.getMonth() == tooltipItems[0].index" : "now.getMonth() == " + (Model.Month - 1) + " && now.getDate() == (tooltipItems[0].index + 1)")
Bryan Lewis
  • 5,629
  • 4
  • 39
  • 45