Search code examples
jqueryasp.net-mvchtmlrazorhtml-encode

How to Render decoded Html from razor view's Javascript


I am trying to Set a textfields values as Raw Html so that it does not encode strings containing characters like single quotes. e.g. O'Riely is displayed as O & # 0 3 9 ; Riely.
The problem i have is I cannot use @html.Raw() function in the Javascript when I also reference a view Model field/mix up with c# code.

<script type="text/javascript">
    $(document).ready(function () {
         <text>
        $('#mytextField').val('@Model.FullName ');
        </text>
    });
</script>

Any Suggestion will be more then welcome.


Solution

  • As answered in this question, you can do this with Html.Raw:

    @Html.Raw(HttpUtility.HtmlDecode(Model.FullName));