How to render HTML string in ASP.NET MVC?

asp.netasp.net Mvc-3Razor

asp.net Problem Overview


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?

asp.net Solutions


Solution 1 - asp.net

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

Solution 2 - asp.net

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

@model YourApp.Models.YourModel
....

@Html.Raw(@Model.NameOfYourProperty)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncameronView Question on Stackoverflow
Solution 1 - asp.netNico SchertlerView Answer on Stackoverflow
Solution 2 - asp.netHrvojeView Answer on Stackoverflow