Share via


How to render a string with html in a view as plain text.

Question

Tuesday, August 20, 2013 11:55 PM

Hello,

I have a C# string which contains html markup with text. How can I take that string and render it in an MVC view so that it will display as plain text and not html markup?

Thanks in advance, Joe.

 

 

All replies (5)

Wednesday, August 21, 2013 12:28 AM ✅Answered

Pass the C# string to the view as a ViewBag or any other mechanism. Inside the view use the Html.Encode


Wednesday, August 21, 2013 3:37 AM ✅Answered

@Model.HtmlText

@Html.Raw(Model.HtmlText)

 


Wednesday, August 21, 2013 4:38 AM ✅Answered

Razor Expression injects the string as html encoded.So there is no need to use Html.Encode method.

Simply put @YourString which will products html encoded output and that output will be rendered as string and not markup on client side.

If you want to render the string value as markup then use @Htm.Raw which does not encode the value and hence html is injected in the output and is rendered as normal html markup.


Wednesday, August 21, 2013 5:41 AM ✅Answered

this link can be helpful for you

http://stackoverflow.com/questions/9050145/html-encode-decode-c-sharp-mvc4


Wednesday, August 21, 2013 8:52 AM

Wow, thanks guys.