C# Escaping double quotes with String interpolation in Razor?

What you need is to use the seldom seen <text> syntax

e.g.

<h1 @{if (true) { <text>data-selected="Hello world"</text> } }>Hello</h1>

try this:

 <select class="form-control"
        @{ if (field.DependentDropdown) { <text>data-selected="@Model.KeyValues.GetValue(field.Name)"</text> } }>

I'm having a tough time convincing it to work in the ternary operator - feel free to edit answer if you get the syntax right


Wrap the string in a call to the Raw() method on the HtmlHelper class.

<select class="form-control"
    @(field.DependentDropdown ? Html.Raw($"data-selected=\"{Model.KeyValues.GetValue(field.Name)}\"") : "")>