I need to populate an attribute of a form tag generated with Html.BeginForm with a JSON object. I can't find a way to get it to print the attribute value without encoding it. I have tried with and without Html.Raw and in both cases the quotes inside the JSON still get escaped. This occurs with both single and double quotes. The problem in the following example is with the "data-feedback" attribute.
Code:
@using (Html.BeginForm("SendMessage", "ContactForm", FormMethod.Post, new { enctype = "multipart/form-data", id = "ContactForm", role = "form", @class = "form-element", data_toggle = "validator", data_feedback = Html.Raw("{'success': '', 'error': 'glyphicon-exclamation-sign'}") }))
Result:
<form action="/contactform/sendmessage" class="form-element" data-feedback="{'success': '', 'error': 'glyphicon-exclamation-sign'}" data-toggle="validator" enctype="multipart/form-data" id="ContactForm" method="post" role="form">
@Html.Raw(Json.Encode("{...}")))
?BeginForm
helper to prevent double-encoded string in Razor view.