Search code examples
javascriptasp.net-mvcrazor

Unable to set HTML text into InnerHtml property


My application has built in ASP.NET MVC 5. Now, what I am trying to do is that application retrieves the text data from database and generate some html text. So, i can set that html text into a specific div element by assigning innerHtml property. But, when i try to assign that html text into that div it doesn't render. It shows html text.

enter image description here

This is my code

<script type="text/javascript">
    var elem = document.getElementById('discussion');
    elem.innerHTML = "@Server.HtmlDecode(@discussionDiv.ToString())";
</script>

Is there any better way that can i use to set that html text into my dom element?


Solution

  • You should use Html.Raw

    elem.innerHTML = '@Html.Raw(discussionDiv.ToString())';
    

    Since it contains a lot of double quotes " i've opted to use single quotes on the value.