Samantha J T Star
Samantha J T Star

Reputation: 32818

Why does C# code executed from a razor view get encoded?

I would like to execute the following from within a razor view

@Select.Topics(@Model.DataSource, @Model.ExamID, @Model.TopicID)

The code works fine but when it returns everything is encoded:

<select id="TopicID">
                &lt;option value=&#39;00&#39;&gt;All Topics&lt;/option&gt;&lt;option value=&#39;01&#39;  selected=&#39;selected&#39; 
        </select>

I cannot change my C# code as it is used in other places. How can I make the view accept exactly what I need? I seem to remember something about RenderAction would that be an option?

Upvotes: 2

Views: 213

Answers (1)

MikeSW
MikeSW

Reputation: 16368

The output is encoded to prevent html code injection. To bypass the encoding use @Html.Raw("[html string]") helper, Btw you shouldn't store the options as html tags. Store only the values and then use @Html.DropDownList helper

Upvotes: 4

Related Questions