1

I am having some trouble when printing javascript code that is created on the server side. The code must be placed in my html, but asp always escapes the string in such a way that my javscript doesn't execute. Here is my code:

//file "generateCode.ascx"
<%
    if (Model.Overrides != null) {                        
        if (Model.Overrides.JavascriptAposTabela != null) {
            %>
            <%: Model.Overrides.JavascriptAposTabela; %>
            <%                            
        }
    }
%>

Am example of the result of the code above is:

function getGreenToRed(percent){
    r = percent&lt;50 ? 255 : Math.floor(255-(percent*2-100)*255/100);
    g = percent&gt;50 ? 255 : Math.floor((percent*2)*255/100);
    return &quot;rgb(&quot;+r+&quot;,&quot;+g+&quot;,0)&quot;;
}

$(&quot;.colormeter&quot;).css(&quot;background-color&quot;, getGreenToRed($(this).text()));

This is not the expected result. I tried the solution proposed in these threads, but it did not work for me:

How do I prevent MVC3 html escaping my validation messages?

Stop the tag builder escaping single quotes ASP.NET MVC 2

I am using asp.net mvc 2

Community
  • 1
  • 1
  • 1
    have you read this? http://stackoverflow.com/questions/5314951/how-to-render-encoded-tags-as-proper-html-rather-than-text – Hunyo Silaw Apr 11 '14 at 12:21

1 Answers1

0

Well. Unfortunately, it was only ignorance of my part.

The solution is pretty simple:

//file "generateCode.ascx"
<%
    if (Model.Overrides != null) {                        
        if (Model.Overrides.JavascriptAposTabela != null) {
            %>
            <%= Model.Overrides.JavascriptAposTabela; %>
            <%                            
        }
    }
%>