Search code examples
c#asp.net-mvcjquery-templatesmvchtmlstring

Using MvcHtmlString with jQuery templates - what's the correct razor syntax?


I have such jQuery template:

<script id="msgTmpl" type="text/x-jquery-tmpl">
    <li><span style="color:Black"><strong>${user.name}</strong> (${datetime})</span><br/><span>${richMessage}</span></li>
</script>

And I use it in such way:

        $.post("/GetMessages", null, function (data, s) {

            if (data.messages) {
                $('#msgTmpl').tmpl(data.messages).appendTo('#chatList');
            }

        ... and so on.

richMessage can contain HTML tags.

I want it to be interpreted as HTML not to be displayed as sequence of tags...

I know MVCHtmlString.Create has to be used but I can not figure out the correct syntax.

I also tried to use [AllowHtml] attribute on richMessage property but with no success.

My problem is: how to use MvcHtmlString.Create to allow MVC display richMessage correctly?


Solution

  • Try using {{html richMessage}} instead of ${richMessage}. Your problem lies with the fact that ${} encodes values by default.