Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string with HTML tags in ASP.NET MVC

Tags:

asp.net-mvc

Is there a way to pass a string with HTML tags without changing the Razor code?

Scenario (current code):

string msg = "<a href='http://www.google.com/html/'>Google</a>";

OUTPUT:

<a href='http://www.google.com/html/'>Google</a> on the page.

GOAL result:

Link to Google without changing the code "@msg".

like image 434
ombinar Avatar asked Aug 11 '15 18:08

ombinar


People also ask

What is MVC HTML string?

Creates an HTML-encoded string using the specified text value. Determines whether the specified string contains content or is either null or empty.

How do you display a string in HTML code?

If you want to append some HTML text to the document. body then you should use document. body. insertAdjacentHTML('beforeend', str); .

What is HTML DisplayFor?

DisplayFor() The DisplayFor() helper method is a strongly typed extension method. It generates a html string for the model object property specified using a lambda expression.


3 Answers

try @Html.Raw(HttpUtility.HtmlDecode(msg));

like image 97
Shivendra Avatar answered Oct 21 '22 06:10

Shivendra


You can try with

HtmlString msg = new HtmlString("<a href='http://www.google.com/html/'>Google</a>");

instead of

string msg = "<a href='http://www.google.com/html/'>Google</a>";
like image 34
davcs86 Avatar answered Oct 21 '22 05:10

davcs86


Hey You can edit your razor code as:

@{

     HtmlString msg = new HtmlString("Hello <br> Hello Again");
     <p style="text-align:justify;"> @msg  </p>
 } 

It's simple

like image 26
Ch Abdul Mannan Avatar answered Oct 21 '22 05:10

Ch Abdul Mannan