Search code examples
asp.net-mvchtml-encode

About html Decode and Encode type ASP.NET MVC


I have a snippet <h2>SomeText</h2>

When I saved in database from ckeditor change to it: &lt;h2&gt;SomeText&lt;/h2&gt;

now when I am using @WebUtility.HtmlDecode alone it's not display correctly and change to it: &amp;lt;h2&amp;gt;SomeText&amp;lt;/h2&amp;gt;

So When I am using @Html.Raw alone it's not display correctly and chage to it: &lt;h2&gt;SomeText&lt;/h2&gt;

Now:

I can't understand why did not work alone when I am using @Html.Raw or @WebUtility.HtmlDecode? but when I am using First @WebUtility.HtmlDecode and next @Html.Raw together it's work correctly like it. @Html.Raw(WebUtility.HtmlDecode(@Item.Content))

&lt;h2&gt;SomeText&lt;/h2&gt; Stored in @Item.Content


Solution

  • Simply use

    @Html.Raw("&lt;h2&gt;SomeText&lt;/h2&gt;")
    

    this will decode the encoded data to html and will store it in DB u r trying to decode and thn u r again encoding it thats the reason u getting the html encode data in DB