Developing a site on ASP MVC4. There is a code in the view that generates the text selected by the style.

function coloringSames(text, sames) { var result = text; for (var i = sames.length - 1; i >= 0; i--) { result = text.substring(0, sames[i].StartPos) + '</span><span class="same-color">' + text.substring(sames[i].StartPos, sames[i].EndPos + 1) + '</span><span>' + text.substring(sames[i].EndPos + 1, text.length - 1); } return '<span>' + result + '</span>'; } 

The function returns normal markup, but when inserted into the page

 document.getElementById("textWork").innerHTML = coloringSames(document.getElementById("text").innerText, webPage.SameSubstringsOriginal); 

The symbols '<' and '>' are replaced by & lt and & lg. Why is this happening and how to solve this problem?

  • one
    innerText makes this transformation. Try using innerHTML instead. - zhenyab
  • It turns out the whole thing in textarea, which I assign innerHTML. it converts characters. - Yura_kukasyan

1 answer 1

Use the MvcHtmlString class specifically designed for this task.