DevDav
DevDav

Reputation: 346

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?

Upvotes: 2

Views: 870

Answers (2)

lalibi
lalibi

Reputation: 3078

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

Upvotes: 2

Kirk Woll
Kirk Woll

Reputation: 77576

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.

Upvotes: 2

Related Questions