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<50 ? 255 : Math.floor(255-(percent*2-100)*255/100);
g = percent>50 ? 255 : Math.floor((percent*2)*255/100);
return "rgb("+r+","+g+",0)";
}
$(".colormeter").css("background-color", 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