6

I have a bit of code in my markup like this :

<% Response.Write(((String)objRow["Post"]).Trim()); %>

The value in objRow["Post"] is HTML markup - similar to this:

<p><span style="color: #ff0000;">asdsada</span></p>

The content of which was created using tinymce. I now want to render the resultant html - basically a view function of a previously created save function.

At the moment, my markup literally spits out the HTML you see there onto my website - but what I really want is a red line of text saying asdsada.

Please help

4 Answers 4

8

it sounds like the value you are trying to display is already html encoded. try this:

<%= HttpUtility.HtmlDecode((string)objRow["Post"]) %>
Sign up to request clarification or add additional context in comments.

2 Comments

This throws the following error: CS1502: The best overloaded method match for 'System.Web.HttpUtility.HtmlDecode(string)' has some invalid arguments
add the cast to string
7

My version of Asp.net is little bit higher, but this might help someone, who uses .net Core 2.0 MVC.

I am using the following approach for rendering html in my Asp.Net Core 2.0 MVC project (the html is created via Tiny MCE and saved in the database, after that it is rendered on the Razor View).

Please, try these two steps:

1) include System.Net in _ViewImports.cshtml:

@using System.Net
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

2) render the html using HtmlDecode in your View.cshtml:

@Html.Raw(@WebUtility.HtmlDecode(Model.MyHtmlData))

This works for me, hope it helps!

But without HtmlDecode, Html.Raw displayed MyHtmlData with html tags (didn't render html, just displayed it). I guess, that is because the data was HTML encoded already, as Zdravko Danev mentioned:

@Html.Raw(Model.MyHtmlData)

Comments

0

You can use literal control in asp.net to display the text and it will render it accordingly.

literal1.text = htmlcodeuwanttodisplay

Comments

-3

Did you try something like

Server.HtmlEncode("<p><span style='color: #ff0000;'>asdsada</span></p>");      

1 Comment

Check your answer before you post it here please. this won't solve the problem. HtmlDecode is the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.