Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 537
cameron Avatar asked Mar 21 '13 13:03

cameron


People also ask

What method is used to render HTML string in a view?

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

Which ASP.NET MVC object generates the HTML output?

The ActionLink() and DisplayNameFor() are extension methods included in the HtmlHelper class. The HtmlHelper class generates HTML elements. For example, @Html.

What is HTML raw in MVC?

The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor. Configuring Bundles. Please refer the following article for complete information on how to configure Bundles in ASP.Net MVC project. Using Bundles (ScriptBundle) in ASP.Net MVC Razor.

What is rendering in ASP.NET MVC?

Render actions are methods that the view calls back in the controller. A strong design point about MVC is the neat separation of controller and view. In this regard, render actions just break this separation. Render actions are effective to use; balancing design with effective solutions is the developer's job.


1 Answers

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

like image 63
Nico Schertler Avatar answered Sep 21 '22 20:09

Nico Schertler