How to render HTML string in ASP.NET MVC?

On my main page, I have the code @{Html.RenderPartial("_Partial1.cshtml");}, and on my Partial, I have an HTML string:

@{ 
    // The string is actually dynamic, not static. This is here for simplicity
    string abc="<div class="error">abc</div>";
} 
@abc

I want to output abc with some CSS error styles, but I actually got <div class="error">abc</div> – of course, no styles there. How do I make it interpreted as HTML source code and not a string?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use the Html.Raw() method for that.

Method 2

And if you are using model in your view than use:

@model YourApp.Models.YourModel
....

@Html.Raw(@Model.NameOfYourProperty)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x