2

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
DevDav
  • 346
  • 1
  • 5
  • 17

2 Answers2

2

You should use the {{html property}} syntax of jQuery templates. This is simialr to @Html.Raw on the MVC side.

<span>{{html richMessage}}</span>

This will output the contents of the richMessage property into the span tag without escaping the HTML.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
2

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

lalibi
  • 3,057
  • 3
  • 33
  • 41